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

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

Issue 2387603003: Resume a backgrounded renderer that was purged and suspended (Closed)
Patch Set: Added transition: => RUNNING 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 // The backgrounded renderer is allowed to be suspended for this duration
88 // before resume.
haraken 2016/10/20 16:52:16 A suspended renderer is suspended for this duratio
89 constexpr base::TimeDelta kMaxTimeRendererAllowedToBeSuspendedBeforeResume =
haraken 2016/10/20 16:52:16 kDurationOfRendererSuspension ?
90 base::TimeDelta::FromSeconds(120);
91
92 // When the suspended backgrounded renderer is resumed, the renderer runs
93 // for this duration.
haraken 2016/10/20 16:52:16 A resumed renderer is resumed for this duration.
94 constexpr base::TimeDelta kSuspendedRendererLengthOfResumption =
haraken 2016/10/20 16:52:16 kDurationOfRendererResumption ?
95 base::TimeDelta::FromSeconds(10);
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 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 421
412 bool TabManager::IsTabAutoDiscardable(content::WebContents* contents) const { 422 bool TabManager::IsTabAutoDiscardable(content::WebContents* contents) const {
413 return GetWebContentsData(contents)->IsAutoDiscardable(); 423 return GetWebContentsData(contents)->IsAutoDiscardable();
414 } 424 }
415 425
416 void TabManager::SetTabAutoDiscardableState(content::WebContents* contents, 426 void TabManager::SetTabAutoDiscardableState(content::WebContents* contents,
417 bool state) { 427 bool state) {
418 GetWebContentsData(contents)->SetAutoDiscardableState(state); 428 GetWebContentsData(contents)->SetAutoDiscardableState(state);
419 } 429 }
420 430
431 content::WebContents* TabManager::GetWebContentsByContentsId(
432 int64_t tab_contents_id) {
433 TabStripModel* model;
434 int index = FindTabStripModelById(tab_contents_id, &model);
435 if (index == -1)
436 return nullptr;
437 return model->GetWebContentsAt(index);
438 }
439
421 bool TabManager::CanSuspendBackgroundedRenderer(int render_process_id) { 440 bool TabManager::CanSuspendBackgroundedRenderer(int render_process_id) {
422 // A renderer can be suspended if it's not playing media. 441 // A renderer can be suspended if it's not playing media.
423 auto tab_stats = GetUnsortedTabStats(); 442 auto tab_stats = GetUnsortedTabStats();
424 for (auto& tab : tab_stats) { 443 for (auto& tab : tab_stats) {
425 if (tab.child_process_host_id != render_process_id) 444 if (tab.child_process_host_id != render_process_id)
426 continue; 445 continue;
427 TabStripModel* model; 446 WebContents* web_contents = GetWebContentsByContentsId(tab.tab_contents_id);
428 int index = FindTabStripModelById(tab.tab_contents_id, &model); 447 if (!web_contents)
429 if (index == -1)
430 return false; 448 return false;
431 WebContents* web_contents = model->GetWebContentsAt(index);
432 if (IsMediaTab(web_contents)) 449 if (IsMediaTab(web_contents))
433 return false; 450 return false;
434 } 451 }
435 return true; 452 return true;
436 } 453 }
437 454
438 // static 455 // static
439 bool TabManager::CompareTabStats(const TabStats& first, 456 bool TabManager::CompareTabStats(const TabStats& first,
440 const TabStats& second) { 457 const TabStats& second) {
441 // Being currently selected is most important to protect. 458 // Being currently selected is most important to protect.
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 697
681 #if defined(OS_CHROMEOS) 698 #if defined(OS_CHROMEOS)
682 TabStatsList stats_list = GetTabStats(); 699 TabStatsList stats_list = GetTabStats();
683 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj. 700 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj.
684 delegate_->AdjustOomPriorities(stats_list); 701 delegate_->AdjustOomPriorities(stats_list);
685 #endif 702 #endif
686 703
687 PurgeAndSuspendBackgroundedTabs(); 704 PurgeAndSuspendBackgroundedTabs();
688 } 705 }
689 706
707 bool TabManager::ShouldUpdatePurgeAndSuspendState(
708 const base::TimeTicks& current_time,
709 const TabStats& tab,
710 const base::TimeDelta& purge_and_suspend_threshold,
711 PurgeAndSuspendState* next_state) {
712 DCHECK(next_state);
713 content::WebContents* content =
714 GetWebContentsByContentsId(tab.tab_contents_id);
715 if (!content)
716 return false;
717
718 base::TimeTicks last_modified_time =
719 GetWebContentsData(content)->LastPurgeAndSuspendModifiedTime();
720 PurgeAndSuspendState state =
721 GetWebContentsData(content)->GetPurgeAndSuspendState();
722 DCHECK(state == RUNNING || tab.last_active < last_modified_time);
haraken 2016/10/20 16:52:16 Add a comment about what 'tab.last_active < last_m
723
724 auto time_passed = current_time - last_modified_time;
725 switch (state) {
726 case RUNNING:
727 if (time_passed > purge_and_suspend_threshold) {
728 *next_state = SUSPENDED;
729 return true;
730 }
731 break;
732 case RESUMED:
733 if (time_passed > kSuspendedRendererLengthOfResumption) {
734 *next_state = SUSPENDED;
735 return true;
736 }
737 break;
738 case SUSPENDED:
739 if (time_passed > kMaxTimeRendererAllowedToBeSuspendedBeforeResume) {
740 *next_state = RESUMED;
741 return true;
742 }
743 break;
744 }
745 return false;
746 }
747
690 void TabManager::PurgeAndSuspendBackgroundedTabs() { 748 void TabManager::PurgeAndSuspendBackgroundedTabs() {
691 const base::CommandLine& command_line = 749 const base::CommandLine& command_line =
692 *base::CommandLine::ForCurrentProcess(); 750 *base::CommandLine::ForCurrentProcess();
693 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) 751 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime))
694 return; 752 return;
695 int purge_and_suspend_time = 0; 753 int purge_and_suspend_time = 0;
696 if (!base::StringToInt( 754 if (!base::StringToInt(
697 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), 755 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime),
698 &purge_and_suspend_time)) { 756 &purge_and_suspend_time)) {
699 return; 757 return;
700 } 758 }
701 if (purge_and_suspend_time <= 0) 759 if (purge_and_suspend_time <= 0)
702 return; 760 return;
703 auto purge_and_suspend_time_threshold = 761 base::TimeTicks current_time = NowTicks();
704 NowTicks() - base::TimeDelta::FromSeconds(purge_and_suspend_time); 762 base::TimeDelta purge_and_suspend_threshold =
763 base::TimeDelta::FromSeconds(purge_and_suspend_time);
705 auto tab_stats = GetUnsortedTabStats(); 764 auto tab_stats = GetUnsortedTabStats();
706 for (auto& tab : tab_stats) { 765 for (auto& tab : tab_stats) {
707 if (!tab.render_process_host->IsProcessBackgrounded()) 766 if (!tab.render_process_host->IsProcessBackgrounded())
708 continue; 767 continue;
768 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id))
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_active > purge_and_suspend_time_threshold) 775 PurgeAndSuspendState state = RUNNING;
776 if (!ShouldUpdatePurgeAndSuspendState(current_time, tab,
haraken 2016/10/20 16:52:16 How about just making this method return the next
777 purge_and_suspend_threshold, &state))
714 continue; 778 continue;
715 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id)) 779 WebContents* content = GetWebContentsByContentsId(tab.tab_contents_id);
716 continue; 780 GetWebContentsData(content)->SetPurgeAndSuspendState(state);
717 tab.render_process_host->PurgeAndSuspend(); 781 switch (state) {
782 case SUSPENDED:
783 tab.render_process_host->PurgeAndSuspend();
784 break;
785 case RESUMED:
786 tab.render_process_host->Resume();
787 break;
788 case RUNNING:
789 NOTREACHED();
790 }
718 } 791 }
719 } 792 }
720 793
721 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) { 794 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) {
722 // Can't discard active index. 795 // Can't discard active index.
723 if (model->active_index() == index) 796 if (model->active_index() == index)
724 return nullptr; 797 return nullptr;
725 798
726 WebContents* old_contents = model->GetWebContentsAt(index); 799 WebContents* old_contents = model->GetWebContentsAt(index);
727 800
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 data->SetRecentlyAudible(current_state); 881 data->SetRecentlyAudible(current_state);
809 data->SetLastAudioChangeTime(NowTicks()); 882 data->SetLastAudioChangeTime(NowTicks());
810 } 883 }
811 } 884 }
812 885
813 void TabManager::ActiveTabChanged(content::WebContents* old_contents, 886 void TabManager::ActiveTabChanged(content::WebContents* old_contents,
814 content::WebContents* new_contents, 887 content::WebContents* new_contents,
815 int index, 888 int index,
816 int reason) { 889 int reason) {
817 GetWebContentsData(new_contents)->SetDiscardState(false); 890 GetWebContentsData(new_contents)->SetDiscardState(false);
891 GetWebContentsData(new_contents)->SetPurgeAndSuspendState(RUNNING);
818 // If |old_contents| is set, that tab has switched from being active to 892 // If |old_contents| is set, that tab has switched from being active to
819 // inactive, so record the time of that transition. 893 // inactive, so record the time of that transition.
820 if (old_contents) 894 if (old_contents)
821 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks()); 895 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks());
822 } 896 }
823 897
824 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, 898 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model,
825 content::WebContents* contents, 899 content::WebContents* contents,
826 int index, 900 int index,
827 bool foreground) { 901 bool foreground) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 // platform. 1033 // platform.
960 std::string allow_multiple_discards = variations::GetVariationParamValue( 1034 std::string allow_multiple_discards = variations::GetVariationParamValue(
961 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); 1035 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards");
962 return (allow_multiple_discards != "true"); 1036 return (allow_multiple_discards != "true");
963 #else 1037 #else
964 return false; 1038 return false;
965 #endif 1039 #endif
966 } 1040 }
967 1041
968 } // namespace memory 1042 } // namespace memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698