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

Side by Side Diff: chrome/browser/memory/tab_manager.cc

Issue 2387603003: Resume a backgrounded renderer that was purged and suspended (Closed)
Patch Set: Rebaselined Created 4 years, 2 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/memory/tab_manager.h" 5 #include "chrome/browser/memory/tab_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <set> 10 #include <set>
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 // For each period of this length record a statistic to indicate whether or not 77 // For each period of this length record a statistic to indicate whether or not
78 // the user experienced a low memory event. If this interval is changed, 78 // the user experienced a low memory event. If this interval is changed,
79 // Tabs.Discard.DiscardInLastMinute must be replaced with a new statistic. 79 // Tabs.Discard.DiscardInLastMinute must be replaced with a new statistic.
80 const int kRecentTabDiscardIntervalSeconds = 60; 80 const int kRecentTabDiscardIntervalSeconds = 60;
81 #endif 81 #endif
82 82
83 // If there has been no priority adjustment in this interval, assume the 83 // If there has been no priority adjustment in this interval, assume the
84 // machine was suspended and correct the timing statistics. 84 // machine was suspended and correct the timing statistics.
85 const int kSuspendThresholdSeconds = kAdjustmentIntervalSeconds * 4; 85 const int kSuspendThresholdSeconds = kAdjustmentIntervalSeconds * 4;
86 86
87 // If a backgrounded renderer is kept backgrounded for this time after
88 // being suspended, resume the renderer to avoid breaking web.
89 constexpr base::TimeDelta kSuspendResumedRendererTime =
90 base::TimeDelta::FromSeconds(10);
91
92 // After resuming and running a backgrounded renderer for this period,
93 // the renderer is purged and suspended again.
94 constexpr base::TimeDelta kResumeSuspendedRendererTime =
95 base::TimeDelta::FromSeconds(120);
Avi (use Gerrit) 2016/10/18 06:15:37 These comments seem to be opposite what you say in
tasak 2016/10/18 10:38:42 Yeah, my comment was completely wrong. Thank you.
96
87 // The time during which a tab is protected from discarding after it stops being 97 // The time during which a tab is protected from discarding after it stops being
88 // audible. 98 // audible.
89 const int kAudioProtectionTimeSeconds = 60; 99 const int kAudioProtectionTimeSeconds = 60;
90 100
91 int FindTabStripModelById(int64_t target_web_contents_id, 101 int FindTabStripModelById(int64_t target_web_contents_id,
92 TabStripModel** model) { 102 TabStripModel** model) {
93 DCHECK(model); 103 DCHECK(model);
94 for (auto* browser : *BrowserList::GetInstance()) { 104 for (auto* browser : *BrowserList::GetInstance()) {
95 TabStripModel* local_model = browser->tab_strip_model(); 105 TabStripModel* local_model = browser->tab_strip_model();
96 for (int idx = 0; idx < local_model->count(); idx++) { 106 for (int idx = 0; idx < local_model->count(); idx++) {
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) 703 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime))
694 return; 704 return;
695 int purge_and_suspend_time = 0; 705 int purge_and_suspend_time = 0;
696 if (!base::StringToInt( 706 if (!base::StringToInt(
697 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), 707 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime),
698 &purge_and_suspend_time)) { 708 &purge_and_suspend_time)) {
699 return; 709 return;
700 } 710 }
701 if (purge_and_suspend_time <= 0) 711 if (purge_and_suspend_time <= 0)
702 return; 712 return;
713 base::TimeTicks current_time = NowTicks();
703 auto purge_and_suspend_time_threshold = 714 auto purge_and_suspend_time_threshold =
704 NowTicks() - base::TimeDelta::FromSeconds(purge_and_suspend_time); 715 current_time - base::TimeDelta::FromSeconds(purge_and_suspend_time);
705 auto tab_stats = GetUnsortedTabStats(); 716 auto tab_stats = GetUnsortedTabStats();
706 for (auto& tab : tab_stats) { 717 for (auto& tab : tab_stats) {
707 if (!tab.render_process_host->IsProcessBackgrounded()) 718 if (!tab.render_process_host->IsProcessBackgrounded())
708 continue; 719 continue;
709 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without 720 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without
710 // timers for simplicity, so PurgeAndSuspend is called even after the 721 // timers for simplicity, so PurgeAndSuspend is called even after the
711 // renderer is purged and suspended once. This should be replaced with 722 // renderer is purged and suspended once. This should be replaced with
712 // timers if we want necessary and sufficient signals. 723 // timers if we want necessary and sufficient signals.
713 if (tab.last_active > purge_and_suspend_time_threshold) 724 if (tab.last_active > purge_and_suspend_time_threshold)
714 continue; 725 continue;
715 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id)) 726 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id))
716 continue; 727 continue;
717 tab.render_process_host->PurgeAndSuspend(); 728
729 auto last_purged_and_suspended_time =
730 tab.render_process_host->GetLastPurgedAndSuspendedTime();
731 auto last_resumed_time =
732 tab.render_process_host->GetLastResumedInBackgroundTime();
733
734 if (last_purged_and_suspended_time < tab.last_active ||
735 (last_resumed_time > last_purged_and_suspended_time &&
736 last_purged_and_suspended_time > tab.last_active &&
737 ((current_time - last_resumed_time) > kSuspendResumedRendererTime))) {
Avi (use Gerrit) 2016/10/18 06:15:36 Holy moly. I just spent twenty minutes pondering t
tasak 2016/10/18 10:38:42 Yeah... the code was dirty... So I add "purge-and
738 tab.render_process_host->PurgeAndSuspend();
739 if (!task_runner_.get())
740 task_runner_ = base::ThreadTaskRunnerHandle::Get();
741 } else {
742 auto delta = current_time - last_purged_and_suspended_time;
743 if (last_resumed_time < last_purged_and_suspended_time &&
744 delta > kResumeSuspendedRendererTime)
745 tab.render_process_host->Resume();
746 }
718 } 747 }
719 } 748 }
720 749
721 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) { 750 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) {
722 // Can't discard active index. 751 // Can't discard active index.
723 if (model->active_index() == index) 752 if (model->active_index() == index)
724 return nullptr; 753 return nullptr;
725 754
726 WebContents* old_contents = model->GetWebContentsAt(index); 755 WebContents* old_contents = model->GetWebContentsAt(index);
727 756
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 // platform. 988 // platform.
960 std::string allow_multiple_discards = variations::GetVariationParamValue( 989 std::string allow_multiple_discards = variations::GetVariationParamValue(
961 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); 990 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards");
962 return (allow_multiple_discards != "true"); 991 return (allow_multiple_discards != "true");
963 #else 992 #else
964 return false; 993 return false;
965 #endif 994 #endif
966 } 995 }
967 996
968 } // namespace memory 997 } // namespace memory
OLDNEW
« no previous file with comments | « no previous file | content/browser/renderer_host/render_process_host_impl.h » ('j') | content/public/browser/render_process_host.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698