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

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

Issue 2462513002: Add Resume logic of Purge+Suspend to TabManager. (Closed)
Patch Set: Fixed. Created 4 years, 1 month 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 // A suspended renderer is suspended for this duration.
88 constexpr base::TimeDelta kDurationOfRendererSuspension =
89 base::TimeDelta::FromSeconds(120);
90
91 // A resumed renderer is resumed for this duration.
92 constexpr base::TimeDelta kDurationOfRendererResumption =
93 base::TimeDelta::FromSeconds(10);
94
87 // The time during which a tab is protected from discarding after it stops being 95 // The time during which a tab is protected from discarding after it stops being
88 // audible. 96 // audible.
89 const int kAudioProtectionTimeSeconds = 60; 97 const int kAudioProtectionTimeSeconds = 60;
90 98
91 int FindTabStripModelById(int64_t target_web_contents_id, 99 int FindTabStripModelById(int64_t target_web_contents_id,
92 TabStripModel** model) { 100 TabStripModel** model) {
93 DCHECK(model); 101 DCHECK(model);
94 for (auto* browser : *BrowserList::GetInstance()) { 102 for (auto* browser : *BrowserList::GetInstance()) {
95 TabStripModel* local_model = browser->tab_strip_model(); 103 TabStripModel* local_model = browser->tab_strip_model();
96 for (int idx = 0; idx < local_model->count(); idx++) { 104 for (int idx = 0; idx < local_model->count(); idx++) {
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 419
412 bool TabManager::IsTabAutoDiscardable(content::WebContents* contents) const { 420 bool TabManager::IsTabAutoDiscardable(content::WebContents* contents) const {
413 return GetWebContentsData(contents)->IsAutoDiscardable(); 421 return GetWebContentsData(contents)->IsAutoDiscardable();
414 } 422 }
415 423
416 void TabManager::SetTabAutoDiscardableState(content::WebContents* contents, 424 void TabManager::SetTabAutoDiscardableState(content::WebContents* contents,
417 bool state) { 425 bool state) {
418 GetWebContentsData(contents)->SetAutoDiscardableState(state); 426 GetWebContentsData(contents)->SetAutoDiscardableState(state);
419 } 427 }
420 428
429 content::WebContents* TabManager::GetWebContentsById(int64_t tab_contents_id) {
430 TabStripModel* model;
chrisha 2016/11/01 21:22:56 = nullptr
tasak 2016/11/02 04:19:46 Done.
431 int index = FindTabStripModelById(tab_contents_id, &model);
432 if (index == -1)
433 return nullptr;
434 return model->GetWebContentsAt(index);
435 }
436
421 bool TabManager::CanSuspendBackgroundedRenderer(int render_process_id) { 437 bool TabManager::CanSuspendBackgroundedRenderer(int render_process_id) {
422 // A renderer can be suspended if it's not playing media. 438 // A renderer can be suspended if it's not playing media.
423 auto tab_stats = GetUnsortedTabStats(); 439 auto tab_stats = GetUnsortedTabStats();
424 for (auto& tab : tab_stats) { 440 for (auto& tab : tab_stats) {
425 if (tab.child_process_host_id != render_process_id) 441 if (tab.child_process_host_id != render_process_id)
426 continue; 442 continue;
427 TabStripModel* model; 443 WebContents* web_contents = GetWebContentsById(tab.tab_contents_id);
428 int index = FindTabStripModelById(tab.tab_contents_id, &model); 444 if (!web_contents)
429 if (index == -1)
430 return false; 445 return false;
431 WebContents* web_contents = model->GetWebContentsAt(index);
432 if (IsMediaTab(web_contents)) 446 if (IsMediaTab(web_contents))
433 return false; 447 return false;
434 } 448 }
435 return true; 449 return true;
436 } 450 }
437 451
438 // static 452 // static
439 bool TabManager::CompareTabStats(const TabStats& first, 453 bool TabManager::CompareTabStats(const TabStats& first,
440 const TabStats& second) { 454 const TabStats& second) {
441 // Being currently selected is most important to protect. 455 // Being currently selected is most important to protect.
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 694
681 #if defined(OS_CHROMEOS) 695 #if defined(OS_CHROMEOS)
682 TabStatsList stats_list = GetTabStats(); 696 TabStatsList stats_list = GetTabStats();
683 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj. 697 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj.
684 delegate_->AdjustOomPriorities(stats_list); 698 delegate_->AdjustOomPriorities(stats_list);
685 #endif 699 #endif
686 700
687 PurgeAndSuspendBackgroundedTabs(); 701 PurgeAndSuspendBackgroundedTabs();
688 } 702 }
689 703
704 TabManager::PurgeAndSuspendState TabManager::GetNextPurgeAndSuspendState(
705 content::WebContents* content,
706 const base::TimeTicks& current_time,
707 const base::TimeDelta& purge_and_suspend_threshold) const {
708 DCHECK(content);
709 PurgeAndSuspendState state =
710 GetWebContentsData(content)->GetPurgeAndSuspendState();
711
712 auto time_passed =
713 current_time -
chrisha 2016/11/01 21:22:56 Move this to the previous line
tasak 2016/11/02 04:19:46 Done.
714 GetWebContentsData(content)->LastPurgeAndSuspendModifiedTime();
715 switch (state) {
716 case RUNNING:
717 if (time_passed > purge_and_suspend_threshold)
718 return SUSPENDED;
719 break;
720 case RESUMED:
721 if (time_passed > kDurationOfRendererResumption)
722 return SUSPENDED;
723 break;
724 case SUSPENDED:
725 if (time_passed > kDurationOfRendererSuspension)
726 return RESUMED;
727 break;
728 }
729 return state;
730 }
731
690 void TabManager::PurgeAndSuspendBackgroundedTabs() { 732 void TabManager::PurgeAndSuspendBackgroundedTabs() {
691 const base::CommandLine& command_line = 733 const base::CommandLine& command_line =
692 *base::CommandLine::ForCurrentProcess(); 734 *base::CommandLine::ForCurrentProcess();
693 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) 735 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime))
694 return; 736 return;
695 int purge_and_suspend_time = 0; 737 int purge_and_suspend_time = 0;
696 if (!base::StringToInt( 738 if (!base::StringToInt(
697 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), 739 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime),
698 &purge_and_suspend_time)) { 740 &purge_and_suspend_time)) {
699 return; 741 return;
700 } 742 }
701 if (purge_and_suspend_time <= 0) 743 if (purge_and_suspend_time <= 0)
702 return; 744 return;
703 auto purge_and_suspend_time_threshold = 745 base::TimeTicks current_time = NowTicks();
704 NowTicks() - base::TimeDelta::FromSeconds(purge_and_suspend_time); 746 base::TimeDelta purge_and_suspend_threshold =
747 base::TimeDelta::FromSeconds(purge_and_suspend_time);
705 auto tab_stats = GetUnsortedTabStats(); 748 auto tab_stats = GetUnsortedTabStats();
706 for (auto& tab : tab_stats) { 749 for (auto& tab : tab_stats) {
707 if (!tab.render_process_host->IsProcessBackgrounded()) 750 if (!tab.render_process_host->IsProcessBackgrounded())
708 continue; 751 continue;
752 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id))
753 continue;
754
755 WebContents* content = GetWebContentsById(tab.tab_contents_id);
756 if (!content)
757 continue;
758
759 PurgeAndSuspendState current_state =
760 GetWebContentsData(content)->GetPurgeAndSuspendState();
761 // If the tab's purge-and-suspend state is not RUNNING, the tab should be
762 // backgrounded. Since tab.last_hidden is updated everytime the tab is
763 // hidden, we should see tab.last_hidden < last_modified_time.
764 DCHECK(current_state == RUNNING ||
765 tab.last_hidden <
766 GetWebContentsData(content)->LastPurgeAndSuspendModifiedTime());
767 PurgeAndSuspendState next_state = GetNextPurgeAndSuspendState(
768 content, current_time, purge_and_suspend_threshold);
769 if (current_state == next_state)
770 continue;
771
709 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without 772 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without
710 // timers for simplicity, so PurgeAndSuspend is called even after the 773 // timers for simplicity, so PurgeAndSuspend is called even after the
711 // renderer is purged and suspended once. This should be replaced with 774 // renderer is purged and suspended once. This should be replaced with
712 // timers if we want necessary and sufficient signals. 775 // timers if we want necessary and sufficient signals.
713 if (tab.last_hidden > purge_and_suspend_time_threshold) 776 GetWebContentsData(content)->SetPurgeAndSuspendState(next_state);
714 continue; 777 switch (next_state) {
715 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id)) 778 case SUSPENDED:
716 continue; 779 tab.render_process_host->PurgeAndSuspend();
717 tab.render_process_host->PurgeAndSuspend(); 780 break;
781 case RESUMED:
782 tab.render_process_host->Resume();
783 break;
784 case RUNNING:
785 NOTREACHED();
786 }
718 } 787 }
719 } 788 }
720 789
721 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) { 790 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) {
722 // Can't discard active index. 791 // Can't discard active index.
723 if (model->active_index() == index) 792 if (model->active_index() == index)
724 return nullptr; 793 return nullptr;
725 794
726 WebContents* old_contents = model->GetWebContentsAt(index); 795 WebContents* old_contents = model->GetWebContentsAt(index);
727 796
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 data->SetRecentlyAudible(current_state); 877 data->SetRecentlyAudible(current_state);
809 data->SetLastAudioChangeTime(NowTicks()); 878 data->SetLastAudioChangeTime(NowTicks());
810 } 879 }
811 } 880 }
812 881
813 void TabManager::ActiveTabChanged(content::WebContents* old_contents, 882 void TabManager::ActiveTabChanged(content::WebContents* old_contents,
814 content::WebContents* new_contents, 883 content::WebContents* new_contents,
815 int index, 884 int index,
816 int reason) { 885 int reason) {
817 GetWebContentsData(new_contents)->SetDiscardState(false); 886 GetWebContentsData(new_contents)->SetDiscardState(false);
887 GetWebContentsData(new_contents)->SetPurgeAndSuspendState(RUNNING);
818 // If |old_contents| is set, that tab has switched from being active to 888 // If |old_contents| is set, that tab has switched from being active to
819 // inactive, so record the time of that transition. 889 // inactive, so record the time of that transition.
820 if (old_contents) 890 if (old_contents)
821 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks()); 891 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks());
822 } 892 }
823 893
824 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, 894 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model,
825 content::WebContents* contents, 895 content::WebContents* contents,
826 int index, 896 int index,
827 bool foreground) { 897 bool foreground) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 // platform. 1029 // platform.
960 std::string allow_multiple_discards = variations::GetVariationParamValue( 1030 std::string allow_multiple_discards = variations::GetVariationParamValue(
961 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); 1031 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards");
962 return (allow_multiple_discards != "true"); 1032 return (allow_multiple_discards != "true");
963 #else 1033 #else
964 return false; 1034 return false;
965 #endif 1035 #endif
966 } 1036 }
967 1037
968 } // namespace memory 1038 } // namespace memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698