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

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

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