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

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
« no previous file with comments | « chrome/browser/memory/tab_manager.h ('k') | chrome/browser/memory/tab_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 = nullptr;
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 base::TimeTicks current_time,
707 const base::TimeDelta& time_to_first_suspension) const {
708 DCHECK(content);
709 PurgeAndSuspendState state =
710 GetWebContentsData(content)->GetPurgeAndSuspendState();
711
712 auto time_passed = current_time -
713 GetWebContentsData(content)->LastPurgeAndSuspendModifiedTime();
714 switch (state) {
715 case RUNNING:
716 if (time_passed > time_to_first_suspension)
717 return SUSPENDED;
718 break;
719 case RESUMED:
720 if (time_passed > kDurationOfRendererResumption)
721 return SUSPENDED;
722 break;
723 case SUSPENDED:
724 if (time_passed > kDurationOfRendererSuspension)
725 return RESUMED;
726 break;
727 }
728 return state;
729 }
730
690 void TabManager::PurgeAndSuspendBackgroundedTabs() { 731 void TabManager::PurgeAndSuspendBackgroundedTabs() {
691 const base::CommandLine& command_line = 732 const base::CommandLine& command_line =
692 *base::CommandLine::ForCurrentProcess(); 733 *base::CommandLine::ForCurrentProcess();
693 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) 734 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime))
694 return; 735 return;
695 int purge_and_suspend_time = 0; 736 int purge_and_suspend_time = 0;
696 if (!base::StringToInt( 737 if (!base::StringToInt(
697 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), 738 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime),
698 &purge_and_suspend_time)) { 739 &purge_and_suspend_time)) {
699 return; 740 return;
700 } 741 }
701 if (purge_and_suspend_time <= 0) 742 if (purge_and_suspend_time <= 0)
702 return; 743 return;
703 auto purge_and_suspend_time_threshold = 744 base::TimeTicks current_time = NowTicks();
704 NowTicks() - base::TimeDelta::FromSeconds(purge_and_suspend_time); 745 base::TimeDelta time_to_first_suspension =
746 base::TimeDelta::FromSeconds(purge_and_suspend_time);
705 auto tab_stats = GetUnsortedTabStats(); 747 auto tab_stats = GetUnsortedTabStats();
706 for (auto& tab : tab_stats) { 748 for (auto& tab : tab_stats) {
707 if (!tab.render_process_host->IsProcessBackgrounded()) 749 if (!tab.render_process_host->IsProcessBackgrounded())
708 continue; 750 continue;
751 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id))
752 continue;
753
754 WebContents* content = GetWebContentsById(tab.tab_contents_id);
755 if (!content)
756 continue;
757
758 PurgeAndSuspendState current_state =
759 GetWebContentsData(content)->GetPurgeAndSuspendState();
760 // If the tab's purge-and-suspend state is not RUNNING, the tab should be
761 // backgrounded. Since tab.last_hidden is updated everytime the tab is
762 // hidden, we should see tab.last_hidden < last_modified_time.
763 DCHECK(current_state == RUNNING ||
764 tab.last_hidden <
765 GetWebContentsData(content)->LastPurgeAndSuspendModifiedTime());
766 PurgeAndSuspendState next_state = GetNextPurgeAndSuspendState(
767 content, current_time, time_to_first_suspension);
768 if (current_state == next_state)
769 continue;
770
709 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without 771 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without
710 // timers for simplicity, so PurgeAndSuspend is called even after the 772 // timers for simplicity, so PurgeAndSuspend is called even after the
711 // renderer is purged and suspended once. This should be replaced with 773 // renderer is purged and suspended once. This should be replaced with
712 // timers if we want necessary and sufficient signals. 774 // timers if we want necessary and sufficient signals.
713 if (tab.last_hidden > purge_and_suspend_time_threshold) 775 GetWebContentsData(content)->SetPurgeAndSuspendState(next_state);
714 continue; 776 switch (next_state) {
715 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id)) 777 case SUSPENDED:
716 continue; 778 tab.render_process_host->PurgeAndSuspend();
717 tab.render_process_host->PurgeAndSuspend(); 779 break;
780 case RESUMED:
781 tab.render_process_host->Resume();
782 break;
783 case RUNNING:
784 NOTREACHED();
785 }
718 } 786 }
719 } 787 }
720 788
721 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) { 789 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) {
722 // Can't discard active index. 790 // Can't discard active index.
723 if (model->active_index() == index) 791 if (model->active_index() == index)
724 return nullptr; 792 return nullptr;
725 793
726 WebContents* old_contents = model->GetWebContentsAt(index); 794 WebContents* old_contents = model->GetWebContentsAt(index);
727 795
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 data->SetRecentlyAudible(current_state); 876 data->SetRecentlyAudible(current_state);
809 data->SetLastAudioChangeTime(NowTicks()); 877 data->SetLastAudioChangeTime(NowTicks());
810 } 878 }
811 } 879 }
812 880
813 void TabManager::ActiveTabChanged(content::WebContents* old_contents, 881 void TabManager::ActiveTabChanged(content::WebContents* old_contents,
814 content::WebContents* new_contents, 882 content::WebContents* new_contents,
815 int index, 883 int index,
816 int reason) { 884 int reason) {
817 GetWebContentsData(new_contents)->SetDiscardState(false); 885 GetWebContentsData(new_contents)->SetDiscardState(false);
886 GetWebContentsData(new_contents)->SetPurgeAndSuspendState(RUNNING);
818 // If |old_contents| is set, that tab has switched from being active to 887 // If |old_contents| is set, that tab has switched from being active to
819 // inactive, so record the time of that transition. 888 // inactive, so record the time of that transition.
820 if (old_contents) 889 if (old_contents)
821 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks()); 890 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks());
822 } 891 }
823 892
824 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, 893 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model,
825 content::WebContents* contents, 894 content::WebContents* contents,
826 int index, 895 int index,
827 bool foreground) { 896 bool foreground) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 // platform. 1028 // platform.
960 std::string allow_multiple_discards = variations::GetVariationParamValue( 1029 std::string allow_multiple_discards = variations::GetVariationParamValue(
961 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); 1030 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards");
962 return (allow_multiple_discards != "true"); 1031 return (allow_multiple_discards != "true");
963 #else 1032 #else
964 return false; 1033 return false;
965 #endif 1034 #endif
966 } 1035 }
967 1036
968 } // namespace memory 1037 } // namespace memory
OLDNEW
« no previous file with comments | « chrome/browser/memory/tab_manager.h ('k') | chrome/browser/memory/tab_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698