Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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. | |
| 89 constexpr base::TimeDelta kMaxTimeRendererAllowedToBeSuspendedBeforeResume = | |
| 90 base::TimeDelta::FromSeconds(120); | |
| 91 | |
| 92 // When the suspended backgrounded renderer is resumed, the renderer runs | |
| 93 // for this duration. | |
| 94 constexpr base::TimeDelta kSuspendedRendererLengthOfResumption = | |
| 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 Loading... | |
| 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 Loading... | |
| 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) { | |
|
Avi (use Gerrit)
2016/10/18 15:41:53
Non-const reference arguments are not allowed. htt
| |
| 712 content::WebContents* content = | |
| 713 GetWebContentsByContentsId(tab.tab_contents_id); | |
| 714 if (!content) | |
| 715 return false; | |
| 716 | |
| 717 base::TimeTicks last_modified_time = | |
| 718 GetWebContentsData(content)->LastPurgeAndSuspendModifiedTime(); | |
| 719 PurgeAndSuspendState state = | |
| 720 GetWebContentsData(content)->GetPurgeAndSuspendState(); | |
| 721 // If the tab was foregrounded, should ignore the returned state. | |
| 722 if (tab.last_active > last_modified_time) | |
| 723 state = BACKGROUNDED; | |
| 724 | |
| 725 auto time_passed = current_time - last_modified_time; | |
| 726 switch (state) { | |
| 727 case BACKGROUNDED: | |
| 728 if (time_passed > purge_and_suspend_threshold) { | |
| 729 next_state = SUSPENDED; | |
| 730 return true; | |
| 731 } | |
| 732 break; | |
| 733 case RESUMED: | |
| 734 if (time_passed > kSuspendedRendererLengthOfResumption) { | |
| 735 next_state = SUSPENDED; | |
| 736 return true; | |
| 737 } | |
| 738 break; | |
| 739 case SUSPENDED: | |
| 740 if (time_passed > kMaxTimeRendererAllowedToBeSuspendedBeforeResume) { | |
| 741 next_state = RESUMED; | |
| 742 return true; | |
| 743 } | |
| 744 break; | |
| 745 } | |
| 746 return false; | |
| 747 } | |
|
Avi (use Gerrit)
2016/10/18 15:41:53
Turning this into state transitions makes this muc
tasak
2016/10/19 06:13:41
Done.
| |
| 748 | |
| 690 void TabManager::PurgeAndSuspendBackgroundedTabs() { | 749 void TabManager::PurgeAndSuspendBackgroundedTabs() { |
| 691 const base::CommandLine& command_line = | 750 const base::CommandLine& command_line = |
| 692 *base::CommandLine::ForCurrentProcess(); | 751 *base::CommandLine::ForCurrentProcess(); |
| 693 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) | 752 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) |
| 694 return; | 753 return; |
| 695 int purge_and_suspend_time = 0; | 754 int purge_and_suspend_time = 0; |
| 696 if (!base::StringToInt( | 755 if (!base::StringToInt( |
| 697 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), | 756 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), |
| 698 &purge_and_suspend_time)) { | 757 &purge_and_suspend_time)) { |
| 699 return; | 758 return; |
| 700 } | 759 } |
| 701 if (purge_and_suspend_time <= 0) | 760 if (purge_and_suspend_time <= 0) |
| 702 return; | 761 return; |
| 703 auto purge_and_suspend_time_threshold = | 762 base::TimeTicks current_time = NowTicks(); |
| 704 NowTicks() - base::TimeDelta::FromSeconds(purge_and_suspend_time); | 763 base::TimeDelta purge_and_suspend_threshold = |
| 764 base::TimeDelta::FromSeconds(purge_and_suspend_time); | |
| 705 auto tab_stats = GetUnsortedTabStats(); | 765 auto tab_stats = GetUnsortedTabStats(); |
| 706 for (auto& tab : tab_stats) { | 766 for (auto& tab : tab_stats) { |
| 707 if (!tab.render_process_host->IsProcessBackgrounded()) | 767 if (!tab.render_process_host->IsProcessBackgrounded()) |
| 708 continue; | 768 continue; |
| 769 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id)) | |
| 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_active > purge_and_suspend_time_threshold) | 776 PurgeAndSuspendState state = BACKGROUNDED; |
| 777 if (!ShouldUpdatePurgeAndSuspendState(current_time, tab, | |
| 778 purge_and_suspend_threshold, state)) | |
| 714 continue; | 779 continue; |
| 715 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id)) | 780 WebContents* content = GetWebContentsByContentsId(tab.tab_contents_id); |
| 716 continue; | 781 GetWebContentsData(content)->SetPurgeAndSuspendState(state); |
| 717 tab.render_process_host->PurgeAndSuspend(); | 782 switch (state) { |
| 783 case SUSPENDED: | |
| 784 tab.render_process_host->PurgeAndSuspend(); | |
| 785 if (!task_runner_.get()) | |
| 786 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 787 break; | |
| 788 case RESUMED: | |
| 789 tab.render_process_host->Resume(); | |
| 790 break; | |
| 791 case BACKGROUNDED: | |
| 792 NOTREACHED(); | |
| 793 } | |
|
Avi (use Gerrit)
2016/10/18 15:41:53
So much simpler. 👍
| |
| 718 } | 794 } |
| 719 } | 795 } |
| 720 | 796 |
| 721 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) { | 797 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) { |
| 722 // Can't discard active index. | 798 // Can't discard active index. |
| 723 if (model->active_index() == index) | 799 if (model->active_index() == index) |
| 724 return nullptr; | 800 return nullptr; |
| 725 | 801 |
| 726 WebContents* old_contents = model->GetWebContentsAt(index); | 802 WebContents* old_contents = model->GetWebContentsAt(index); |
| 727 | 803 |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 959 // platform. | 1035 // platform. |
| 960 std::string allow_multiple_discards = variations::GetVariationParamValue( | 1036 std::string allow_multiple_discards = variations::GetVariationParamValue( |
| 961 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); | 1037 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); |
| 962 return (allow_multiple_discards != "true"); | 1038 return (allow_multiple_discards != "true"); |
| 963 #else | 1039 #else |
| 964 return false; | 1040 return false; |
| 965 #endif | 1041 #endif |
| 966 } | 1042 } |
| 967 | 1043 |
| 968 } // namespace memory | 1044 } // namespace memory |
| OLD | NEW |