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

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

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

Powered by Google App Engine
This is Rietveld 408576698