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
3 // found in the LICENSE file.
5 #include "athena/resource_manager/public/resource_manager.h"
10 #include "athena/activity/public/activity.h"
11 #include "athena/activity/public/activity_manager.h"
12 #include "athena/activity/public/activity_manager_observer.h"
13 #include "athena/resource_manager/memory_pressure_notifier.h"
14 #include "athena/resource_manager/public/resource_manager_delegate.h"
15 #include "athena/wm/public/window_list_provider.h"
16 #include "athena/wm/public/window_list_provider_observer.h"
17 #include "athena/wm/public/window_manager.h"
18 #include "athena/wm/public/window_manager_observer.h"
19 #include "base/logging.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/time/time.h"
22 #include "ui/aura/window.h"
27 class ResourceManagerImpl : public ResourceManager,
28 public WindowManagerObserver,
29 public ActivityManagerObserver,
30 public MemoryPressureObserver,
31 public WindowListProviderObserver {
33 ResourceManagerImpl(ResourceManagerDelegate* delegate);
34 virtual ~ResourceManagerImpl();
37 virtual void SetMemoryPressureAndStopMonitoring(
38 MemoryPressureObserver::MemoryPressure pressure) OVERRIDE;
39 virtual void SetWaitTimeBetweenResourceManageCalls(int time_in_ms) OVERRIDE {
40 wait_time_for_resource_deallocation_ =
41 base::TimeDelta::FromMilliseconds(time_in_ms);
42 // Reset the timeout to force the next resource call to execute immediately.
43 next_resource_management_time_ = base::Time::Now();
46 virtual void Pause(bool pause) OVERRIDE {
49 queued_command_ = false;
54 if (!pause && queued_command_) {
55 UpdateActivityOrder();
61 // ActivityManagerObserver:
62 virtual void OnActivityStarted(Activity* activity) OVERRIDE;
63 virtual void OnActivityEnding(Activity* activity) OVERRIDE;
65 // WindowManagerObserver:
66 virtual void OnOverviewModeEnter() OVERRIDE;
67 virtual void OnOverviewModeExit() OVERRIDE;
68 virtual void OnSplitViewModeEnter() OVERRIDE;
69 virtual void OnSplitViewModeExit() OVERRIDE;
71 // MemoryPressureObserver:
72 virtual void OnMemoryPressure(
73 MemoryPressureObserver::MemoryPressure pressure) OVERRIDE;
74 virtual ResourceManagerDelegate* GetDelegate() OVERRIDE;
76 // WindowListProviderObserver:
77 virtual void OnWindowStackingChanged() OVERRIDE;
78 virtual void OnWindowRemoved(aura::Window* removed_window,
82 // Manage the resources for our activities.
83 void ManageResource();
85 // Check that the visibility of activities is properly set.
86 void UpdateVisibilityStates();
88 // Check if activities can be unloaded to reduce memory pressure.
89 void TryToUnloadAnActivity();
91 // Order our activity list to the order of activities of the stream.
92 // TODO(skuhne): Once the ActivityManager is responsible to create this list
93 // for us, we can remove this code here.
94 void UpdateActivityOrder();
96 // Resources were released and a quiet period is needed before we release
97 // more since it takes a while to trickle through the system.
98 void OnResourcesReleased();
100 // The memory pressure has increased, previously applied measures did not show
101 // effect and immediate action is required.
102 void OnMemoryPressureIncreased();
104 // Returns true when the previous memory release was long enough ago to try
105 // unloading another activity.
106 bool AllowedToUnloadActivity();
108 // The sorted (new(front) -> old(back)) activity list.
109 // TODO(skuhne): Once the ActivityManager is responsible to create this list
110 // for us, we can remove this code here.
111 std::vector<Activity*> activity_list_;
113 // The resource manager delegate.
114 scoped_ptr<ResourceManagerDelegate> delegate_;
116 // Keeping a reference to the current memory pressure.
117 MemoryPressureObserver::MemoryPressure current_memory_pressure_;
119 // The memory pressure notifier.
120 scoped_ptr<MemoryPressureNotifier> memory_pressure_notifier_;
122 // A ref counter. As long as not 0, the management is on hold.
125 // If true, a command came in while the resource manager was paused.
126 bool queued_command_;
128 // Used by ManageResource() to determine an activity state change while it
129 // changes Activity properties.
130 bool activity_order_changed_;
132 // True if in overview mode - activity order changes will be ignored if true
133 // and postponed till after the overview mode is ending.
134 bool in_overview_mode_;
136 // True if we are in split view mode.
137 bool in_split_view_mode_;
139 // The last time the resource manager was called to release resources.
140 // Avoid too aggressive resource de-allocation by enforcing a wait time of
141 // |wait_time_for_resource_deallocation_| between executed calls.
142 base::Time next_resource_management_time_;
144 // The wait time between two resource managing executions.
145 base::TimeDelta wait_time_for_resource_deallocation_;
147 DISALLOW_COPY_AND_ASSIGN(ResourceManagerImpl);
151 ResourceManagerImpl* instance = NULL;
153 // We allow this many activities to be visible. All others must be at state of
154 // invisible or below.
155 const int kMaxVisibleActivities = 3;
159 ResourceManagerImpl::ResourceManagerImpl(ResourceManagerDelegate* delegate)
160 : delegate_(delegate),
161 current_memory_pressure_(MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN),
162 memory_pressure_notifier_(new MemoryPressureNotifier(this)),
164 queued_command_(false),
165 activity_order_changed_(false),
166 in_overview_mode_(false),
167 in_split_view_mode_(false),
168 next_resource_management_time_(base::Time::Now()),
169 wait_time_for_resource_deallocation_(base::TimeDelta::FromMilliseconds(
170 delegate_->MemoryPressureIntervalInMS())) {
171 WindowManager::Get()->AddObserver(this);
172 WindowManager::Get()->GetWindowListProvider()->AddObserver(this);
173 ActivityManager::Get()->AddObserver(this);
176 ResourceManagerImpl::~ResourceManagerImpl() {
177 ActivityManager::Get()->RemoveObserver(this);
178 WindowManager::Get()->GetWindowListProvider()->RemoveObserver(this);
179 WindowManager::Get()->RemoveObserver(this);
181 while (!activity_list_.empty())
182 OnActivityEnding(activity_list_.front());
185 void ResourceManagerImpl::SetMemoryPressureAndStopMonitoring(
186 MemoryPressureObserver::MemoryPressure pressure) {
187 memory_pressure_notifier_->StopObserving();
188 OnMemoryPressure(pressure);
191 void ResourceManagerImpl::OnActivityStarted(Activity* activity) {
192 // As long as we have to manage the list of activities ourselves, we need to
194 activity_list_.push_back(activity);
195 UpdateActivityOrder();
196 // Update the activity states.
198 // Remember that the activity order has changed.
199 activity_order_changed_ = true;
202 void ResourceManagerImpl::OnActivityEnding(Activity* activity) {
203 DCHECK(activity->GetWindow());
204 // Remove the activity from the list again.
205 std::vector<Activity*>::iterator it =
206 std::find(activity_list_.begin(), activity_list_.end(), activity);
207 DCHECK(it != activity_list_.end());
208 activity_list_.erase(it);
209 // Remember that the activity order has changed.
210 activity_order_changed_ = true;
213 void ResourceManagerImpl::OnOverviewModeEnter() {
214 in_overview_mode_ = true;
217 void ResourceManagerImpl::OnOverviewModeExit() {
218 in_overview_mode_ = false;
219 // Reorder the activities and manage the resources again since an order change
220 // might have caused a visibility change.
221 UpdateActivityOrder();
225 void ResourceManagerImpl::OnSplitViewModeEnter() {
226 // Re-apply the memory pressure to make sure enough items are visible.
227 in_split_view_mode_ = true;
232 void ResourceManagerImpl::OnSplitViewModeExit() {
233 // We don't do immediately something yet. The next ManageResource call will
235 in_split_view_mode_ = false;
238 void ResourceManagerImpl::OnWindowStackingChanged() {
239 activity_order_changed_ = true;
241 queued_command_ = true;
245 // No need to do anything while being in overview mode.
246 if (in_overview_mode_)
249 // As long as we have to manage the list of activities ourselves, we need to
251 UpdateActivityOrder();
253 // Manage the resources of each activity.
257 void ResourceManagerImpl::OnWindowRemoved(aura::Window* removed_window,
261 void ResourceManagerImpl::OnMemoryPressure(
262 MemoryPressureObserver::MemoryPressure pressure) {
263 if (pressure > current_memory_pressure_)
264 OnMemoryPressureIncreased();
265 current_memory_pressure_ = pressure;
269 ResourceManagerDelegate* ResourceManagerImpl::GetDelegate() {
270 return delegate_.get();
273 void ResourceManagerImpl::ManageResource() {
274 // If there is none or only one app running we cannot do anything.
275 if (activity_list_.size() <= 1U)
279 queued_command_ = true;
283 // Check that the visibility of items is properly set. Note that this might
284 // already trigger a release of resources. If this happens,
285 // AllowedToUnloadActivity() will return false.
286 UpdateVisibilityStates();
288 // Since resource deallocation takes time, we avoid to release more resources
289 // in short succession. Note that we come here periodically and if one call
290 // is not triggering an unload, the next one will.
291 if (AllowedToUnloadActivity())
292 TryToUnloadAnActivity();
295 void ResourceManagerImpl::UpdateVisibilityStates() {
296 // The first n activities should be treated as "visible", means they updated
297 // in overview mode and will keep their layer resources for faster switch
298 // times. Usually we use |kMaxVisibleActivities| items, but when the memory
299 // pressure gets critical we only hold as many as are really visible.
300 size_t max_activities = kMaxVisibleActivities;
301 if (current_memory_pressure_ == MEMORY_PRESSURE_CRITICAL)
302 max_activities = in_split_view_mode_ ? 2 : 1;
304 // Restart and / or bail if the order of activities changes due to our calls.
305 activity_order_changed_ = false;
307 // Change the visibility of our activities in a pre-processing step. This is
308 // required since it might change the order/number of activities.
310 while (index < activity_list_.size()) {
311 Activity* activity = activity_list_[index];
312 Activity::ActivityState state = activity->GetCurrentState();
314 // The first |kMaxVisibleActivities| entries should be visible, all others
315 // invisible or at a lower activity state.
316 if (index < max_activities ||
317 (state == Activity::ACTIVITY_INVISIBLE ||
318 state == Activity::ACTIVITY_VISIBLE)) {
319 Activity::ActivityState visiblity_state =
320 index < max_activities ? Activity::ACTIVITY_VISIBLE :
321 Activity::ACTIVITY_INVISIBLE;
322 // Only change the state when it changes. Note that when the memory
323 // pressure is critical, only the primary activities (1 or 2) are made
324 // visible. Furthermore, in relaxed mode we only want to turn visible,
326 if (visiblity_state != state &&
327 (current_memory_pressure_ != MEMORY_PRESSURE_LOW ||
328 visiblity_state == Activity::ACTIVITY_VISIBLE)) {
329 activity->SetCurrentState(visiblity_state);
330 // If we turned an activity invisible, we are already releasing memory
331 // and can hold off releasing more for now.
332 if (visiblity_state == Activity::ACTIVITY_INVISIBLE)
333 OnResourcesReleased();
337 // See which index we should handle next.
338 if (activity_order_changed_) {
339 activity_order_changed_ = false;
347 void ResourceManagerImpl::TryToUnloadAnActivity() {
348 // TODO(skuhne): This algorithm needs to take all kinds of predictive analysis
349 // and running applications into account. For this first patch we only do a
350 // very simple "floating window" algorithm which is surely not good enough.
351 size_t max_running_activities = 5;
352 switch (current_memory_pressure_) {
353 case MEMORY_PRESSURE_UNKNOWN:
354 // If we do not know how much memory we have we assume that it must be a
357 case MEMORY_PRESSURE_HIGH:
358 max_running_activities = 5;
360 case MEMORY_PRESSURE_CRITICAL:
361 max_running_activities = 0;
363 case MEMORY_PRESSURE_MODERATE:
364 max_running_activities = 7;
366 case MEMORY_PRESSURE_LOW:
371 // Check if / which activity we want to unload.
372 Activity* oldest_media_activity = NULL;
373 std::vector<Activity*> unloadable_activities;
374 for (std::vector<Activity*>::iterator it = activity_list_.begin();
375 it != activity_list_.end(); ++it) {
376 Activity::ActivityState state = (*it)->GetCurrentState();
377 // The activity should neither be unloaded nor visible.
378 if (state != Activity::ACTIVITY_UNLOADED &&
379 state != Activity::ACTIVITY_VISIBLE) {
380 if ((*it)->GetMediaState() == Activity::ACTIVITY_MEDIA_STATE_NONE) {
381 // Does not play media - so we can unload this immediately.
382 unloadable_activities.push_back(*it);
384 oldest_media_activity = *it;
389 if (unloadable_activities.size() > max_running_activities) {
390 OnResourcesReleased();
391 unloadable_activities.back()->SetCurrentState(Activity::ACTIVITY_UNLOADED);
393 } else if (current_memory_pressure_ == MEMORY_PRESSURE_CRITICAL) {
394 if (oldest_media_activity) {
395 OnResourcesReleased();
396 oldest_media_activity->SetCurrentState(Activity::ACTIVITY_UNLOADED);
397 LOG(WARNING) << "Unloading item to releave critical memory pressure";
400 LOG(ERROR) << "[ResourceManager]: Single activity uses too much memory.";
404 if (current_memory_pressure_ != MEMORY_PRESSURE_UNKNOWN) {
405 // Only show this warning when the memory pressure is actually known. This
406 // will suppress warnings in e.g. unit tests.
407 LOG(WARNING) << "[ResourceManager]: No way to release memory pressure (" <<
408 current_memory_pressure_ <<
409 "), Activities (running, allowed, unloadable)=(" <<
410 activity_list_.size() << ", " <<
411 max_running_activities << ", " <<
412 unloadable_activities.size() << ")";
416 void ResourceManagerImpl::UpdateActivityOrder() {
417 queued_command_ = true;
418 if (activity_list_.empty())
420 std::vector<Activity*> new_activity_list;
421 const aura::Window::Windows children =
422 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
423 // Find the first window in the container which is part of the application.
424 for (aura::Window::Windows::const_reverse_iterator child_iterator =
426 child_iterator != children.rend(); ++child_iterator) {
427 for (std::vector<Activity*>::iterator activity_iterator =
428 activity_list_.begin();
429 activity_iterator != activity_list_.end(); ++activity_iterator) {
430 if (*child_iterator == (*activity_iterator)->GetWindow()) {
431 new_activity_list.push_back(*activity_iterator);
432 activity_list_.erase(activity_iterator);
437 // At this point the old list should be empty and we can swap the lists.
438 DCHECK(!activity_list_.size());
439 activity_list_ = new_activity_list;
441 // Remember that the activity order has changed.
442 activity_order_changed_ = true;
445 void ResourceManagerImpl::OnResourcesReleased() {
446 // Do not release too many activities in short succession since it takes time
447 // to release resources. As such wait the memory pressure interval before the
449 next_resource_management_time_ = base::Time::Now() +
450 wait_time_for_resource_deallocation_;
453 void ResourceManagerImpl::OnMemoryPressureIncreased() {
454 // By setting the timer to Now, the next call will immediately be performed.
455 next_resource_management_time_ = base::Time::Now();
458 bool ResourceManagerImpl::AllowedToUnloadActivity() {
459 return current_memory_pressure_ != MEMORY_PRESSURE_LOW &&
460 base::Time::Now() >= next_resource_management_time_;
466 void ResourceManager::Create() {
468 instance = new ResourceManagerImpl(
469 ResourceManagerDelegate::CreateResourceManagerDelegate());
473 ResourceManager* ResourceManager::Get() {
478 void ResourceManager::Shutdown() {
484 ResourceManager::ResourceManager() {}
486 ResourceManager::~ResourceManager() {
491 } // namespace athena