OLD | NEW |
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" |
| 12 #include "base/memory/memory_coordinator_proxy.h" |
11 #include "base/memory/memory_pressure_listener.h" | 13 #include "base/memory/memory_pressure_listener.h" |
12 #include "base/memory/memory_pressure_monitor.h" | 14 #include "base/memory/memory_pressure_monitor.h" |
13 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
14 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
15 #include "build/build_config.h" | 17 #include "build/build_config.h" |
16 #include "content/common/host_shared_bitmap_manager.h" | 18 #include "content/common/host_shared_bitmap_manager.h" |
| 19 #include "content/public/common/content_features.h" |
17 | 20 |
18 namespace content { | 21 namespace content { |
19 namespace { | 22 namespace { |
20 | 23 |
21 const int kModeratePressurePercentage = 50; | 24 const int kModeratePressurePercentage = 50; |
22 const int kCriticalPressurePercentage = 10; | 25 const int kCriticalPressurePercentage = 10; |
23 | 26 |
24 } // namespace | 27 } // namespace |
25 | 28 |
26 RendererFrameManager* RendererFrameManager::GetInstance() { | 29 RendererFrameManager* RendererFrameManager::GetInstance() { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 if (locked_count > 1) { | 68 if (locked_count > 1) { |
66 locked_frames_[frame]--; | 69 locked_frames_[frame]--; |
67 } else { | 70 } else { |
68 RemoveFrame(frame); | 71 RemoveFrame(frame); |
69 unlocked_frames_.push_front(frame); | 72 unlocked_frames_.push_front(frame); |
70 CullUnlockedFrames(GetMaxNumberOfSavedFrames()); | 73 CullUnlockedFrames(GetMaxNumberOfSavedFrames()); |
71 } | 74 } |
72 } | 75 } |
73 | 76 |
74 size_t RendererFrameManager::GetMaxNumberOfSavedFrames() const { | 77 size_t RendererFrameManager::GetMaxNumberOfSavedFrames() const { |
75 base::MemoryPressureMonitor* monitor = base::MemoryPressureMonitor::Get(); | 78 int percentage = 100; |
| 79 if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) { |
| 80 switch (base::MemoryCoordinatorProxy::GetInstance()-> |
| 81 GetCurrentMemoryState()) { |
| 82 case base::MemoryState::NORMAL: |
| 83 percentage = 100; |
| 84 break; |
| 85 case base::MemoryState::THROTTLED: |
| 86 percentage = kCriticalPressurePercentage; |
| 87 break; |
| 88 case base::MemoryState::SUSPENDED: |
| 89 case base::MemoryState::UNKNOWN: |
| 90 NOTREACHED(); |
| 91 break; |
| 92 } |
| 93 } else { |
| 94 base::MemoryPressureMonitor* monitor = base::MemoryPressureMonitor::Get(); |
76 | 95 |
77 if (!monitor) | 96 if (!monitor) |
78 return max_number_of_saved_frames_; | 97 return max_number_of_saved_frames_; |
79 | 98 |
80 // Until we have a global OnMemoryPressureChanged event we need to query the | 99 // Until we have a global OnMemoryPressureChanged event we need to query the |
81 // value from our specific pressure monitor. | 100 // value from our specific pressure monitor. |
82 int percentage = 100; | 101 switch (monitor->GetCurrentPressureLevel()) { |
83 switch (monitor->GetCurrentPressureLevel()) { | 102 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
84 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | 103 percentage = 100; |
85 percentage = 100; | 104 break; |
86 break; | 105 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: |
87 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | 106 percentage = kModeratePressurePercentage; |
88 percentage = kModeratePressurePercentage; | 107 break; |
89 break; | 108 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: |
90 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | 109 percentage = kCriticalPressurePercentage; |
91 percentage = kCriticalPressurePercentage; | 110 break; |
92 break; | 111 } |
93 } | 112 } |
94 size_t frames = (max_number_of_saved_frames_ * percentage) / 100; | 113 size_t frames = (max_number_of_saved_frames_ * percentage) / 100; |
95 return std::max(static_cast<size_t>(1), frames); | 114 return std::max(static_cast<size_t>(1), frames); |
96 } | 115 } |
97 | 116 |
98 RendererFrameManager::RendererFrameManager() | 117 RendererFrameManager::RendererFrameManager() |
99 : memory_pressure_listener_( | 118 : memory_pressure_listener_(new base::MemoryPressureListener( |
100 base::Bind(&RendererFrameManager::OnMemoryPressure, | 119 base::Bind(&RendererFrameManager::OnMemoryPressure, |
101 base::Unretained(this))) { | 120 base::Unretained(this)))) { |
102 // Note: With the destruction of this class the |memory_pressure_listener_| | 121 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
103 // gets destroyed and the observer will remove itself. | |
104 max_number_of_saved_frames_ = | 122 max_number_of_saved_frames_ = |
105 #if defined(OS_ANDROID) | 123 #if defined(OS_ANDROID) |
106 // If the amount of memory on the device is >= 3.5 GB, save up to 5 | 124 // If the amount of memory on the device is >= 3.5 GB, save up to 5 |
107 // frames. | 125 // frames. |
108 base::SysInfo::AmountOfPhysicalMemoryMB() < 1024 * 3.5f ? 1 : 5; | 126 base::SysInfo::AmountOfPhysicalMemoryMB() < 1024 * 3.5f ? 1 : 5; |
109 #else | 127 #else |
110 std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256)); | 128 std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256)); |
111 #endif | 129 #endif |
112 max_handles_ = base::SharedMemory::GetHandleLimit() / 8.0f; | 130 max_handles_ = base::SharedMemory::GetHandleLimit() / 8.0f; |
113 } | 131 } |
(...skipping 15 matching lines...) Expand all Loading... |
129 unlocked_frames_.size() + locked_frames_.size() > saved_frame_limit) { | 147 unlocked_frames_.size() + locked_frames_.size() > saved_frame_limit) { |
130 size_t old_size = unlocked_frames_.size(); | 148 size_t old_size = unlocked_frames_.size(); |
131 // Should remove self from list. | 149 // Should remove self from list. |
132 unlocked_frames_.back()->EvictCurrentFrame(); | 150 unlocked_frames_.back()->EvictCurrentFrame(); |
133 DCHECK_EQ(unlocked_frames_.size() + 1, old_size); | 151 DCHECK_EQ(unlocked_frames_.size() + 1, old_size); |
134 } | 152 } |
135 } | 153 } |
136 | 154 |
137 void RendererFrameManager::OnMemoryPressure( | 155 void RendererFrameManager::OnMemoryPressure( |
138 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 156 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
139 int saved_frame_limit = max_number_of_saved_frames_; | |
140 if (saved_frame_limit <= 1) | |
141 return; | |
142 int percentage = 100; | |
143 switch (memory_pressure_level) { | 157 switch (memory_pressure_level) { |
144 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | 158 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: |
145 percentage = kModeratePressurePercentage; | 159 PurgeMemory(kModeratePressurePercentage); |
146 break; | 160 break; |
147 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | 161 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: |
148 percentage = kCriticalPressurePercentage; | 162 PurgeMemory(kCriticalPressurePercentage); |
149 break; | 163 break; |
150 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | 164 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
151 // No need to change anything when there is no pressure. | 165 // No need to change anything when there is no pressure. |
152 return; | 166 return; |
153 } | 167 } |
| 168 } |
| 169 |
| 170 void RendererFrameManager::OnMemoryStateChange(base::MemoryState state) { |
| 171 switch (state) { |
| 172 case base::MemoryState::NORMAL: |
| 173 // It is not necessary to purge here. |
| 174 break; |
| 175 case base::MemoryState::THROTTLED: |
| 176 PurgeMemory(kCriticalPressurePercentage); |
| 177 break; |
| 178 case base::MemoryState::SUSPENDED: |
| 179 // Note that SUSPENDED never occurs in the main browser process so far. |
| 180 // Fall through. |
| 181 case base::MemoryState::UNKNOWN: |
| 182 NOTREACHED(); |
| 183 break; |
| 184 } |
| 185 } |
| 186 |
| 187 void RendererFrameManager::PurgeMemory(int percentage) { |
| 188 int saved_frame_limit = max_number_of_saved_frames_; |
| 189 if (saved_frame_limit <= 1) |
| 190 return; |
154 CullUnlockedFrames(std::max(1, (saved_frame_limit * percentage) / 100)); | 191 CullUnlockedFrames(std::max(1, (saved_frame_limit * percentage) / 100)); |
155 } | 192 } |
156 | 193 |
157 } // namespace content | 194 } // namespace content |
OLD | NEW |