Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_delegate_chromeos.h" | 5 #include "chrome/browser/memory/tab_manager_delegate_chromeos.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 | 69 |
| 70 // Checks if a window renders ARC apps. | 70 // Checks if a window renders ARC apps. |
| 71 bool IsArcWindow(aura::Window* window) { | 71 bool IsArcWindow(aura::Window* window) { |
| 72 if (!window || window->name() != kExoShellSurfaceWindowName) | 72 if (!window || window->name() != kExoShellSurfaceWindowName) |
| 73 return false; | 73 return false; |
| 74 std::string application_id = exo::ShellSurface::GetApplicationId(window); | 74 std::string application_id = exo::ShellSurface::GetApplicationId(window); |
| 75 return base::StartsWith(application_id, kArcProcessNamePrefix, | 75 return base::StartsWith(application_id, kArcProcessNamePrefix, |
| 76 base::CompareCase::SENSITIVE); | 76 base::CompareCase::SENSITIVE); |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool IsArcWindowInForeground() { | |
| 80 auto activation_client = GetActivationClient(); | |
| 81 return activation_client && IsArcWindow(activation_client->GetActiveWindow()); | |
| 82 } | |
| 83 | |
| 84 int AppStateToPriority( | |
| 85 const arc::mojom::ProcessState& process_state) { | |
| 86 // Logic copied from Android: | |
| 87 // frameworks/base/core/java/android/app/ActivityManager.java | |
| 88 // Note that ProcessState enumerates from most important (lower value) to | |
| 89 // least important (higher value), while ProcessPriority enumerates the | |
| 90 // opposite. | |
| 91 if (process_state >= arc::mojom::ProcessState::HOME) { | |
| 92 return ProcessPriority::ANDROID_BACKGROUND; | |
| 93 } else if (process_state >= arc::mojom::ProcessState::SERVICE) { | |
| 94 return ProcessPriority::ANDROID_SERVICE; | |
| 95 } else if (process_state >= arc::mojom::ProcessState::HEAVY_WEIGHT) { | |
| 96 return ProcessPriority::ANDROID_CANT_SAVE_STATE; | |
| 97 } else if (process_state >= arc::mojom::ProcessState::IMPORTANT_BACKGROUND) { | |
| 98 return ProcessPriority::ANDROID_PERCEPTIBLE; | |
| 99 } else if (process_state >= arc::mojom::ProcessState::IMPORTANT_FOREGROUND) { | |
| 100 return ProcessPriority::ANDROID_VISIBLE; | |
| 101 } else if (process_state >= arc::mojom::ProcessState::TOP_SLEEPING) { | |
| 102 return ProcessPriority::ANDROID_TOP_SLEEPING; | |
| 103 } else if (process_state >= arc::mojom::ProcessState::FOREGROUND_SERVICE) { | |
| 104 return ProcessPriority::ANDROID_FOREGROUND_SERVICE; | |
| 105 } else if (process_state >= arc::mojom::ProcessState::TOP) { | |
| 106 return IsArcWindowInForeground() ? | |
| 107 ProcessPriority::ANDROID_TOP : | |
| 108 ProcessPriority::ANDROID_TOP_INACTIVE; | |
| 109 } else if (process_state >= arc::mojom::ProcessState::PERSISTENT) { | |
| 110 return ProcessPriority::ANDROID_PERSISTENT; | |
| 111 } | |
| 112 return ProcessPriority::ANDROID_NON_EXISTS; | |
| 113 } | |
| 114 | |
| 115 int TabStatsToPriority(const TabStats& tab) { | |
| 116 if (tab.is_selected) | |
| 117 return ProcessPriority::CHROME_SELECTED; | |
| 118 | |
| 119 int priority = 0; | |
| 120 | |
| 121 if (tab.is_app) { | |
| 122 priority = ProcessPriority::CHROME_APP; | |
| 123 } else if (tab.is_internal_page) { | |
| 124 priority = ProcessPriority::CHROME_INTERNAL; | |
| 125 } else { | |
| 126 priority = ProcessPriority::CHROME_NORMAL; | |
| 127 } | |
| 128 if (tab.is_pinned) | |
| 129 priority |= ProcessPriority::CHROME_PINNED; | |
| 130 if (tab.is_media) | |
| 131 priority |= ProcessPriority::CHROME_MEDIA; | |
| 132 if (tab.has_form_entry) | |
| 133 priority |= ProcessPriority::CHROME_CANT_SAVE_STATE; | |
| 134 | |
| 135 return priority; | |
| 136 } | |
| 137 | |
| 138 bool IsArcMemoryManagementEnabled() { | 79 bool IsArcMemoryManagementEnabled() { |
| 139 return base::FeatureList::IsEnabled(features::kArcMemoryManagement); | 80 return base::FeatureList::IsEnabled(features::kArcMemoryManagement); |
| 140 } | 81 } |
| 141 | 82 |
| 142 } // namespace | 83 } // namespace |
| 143 | 84 |
| 144 std::ostream& operator<<( | 85 std::ostream& operator<<( |
| 145 std::ostream& out, const TabManagerDelegate::Candidate& candidate) { | 86 std::ostream& out, const TabManagerDelegate::Candidate& candidate) { |
| 146 if (candidate.is_arc_app) { | 87 if (candidate.app()) { |
| 147 out << "app " << candidate.app->pid() | 88 out << "app " << candidate.app()->pid() |
| 148 << " (" << candidate.app->process_name() << ")"; | 89 << " (" << candidate.app()->process_name() << ")" |
| 149 } else { | 90 << ", process_state " << candidate.app()->process_state() |
| 150 out << "tab " << candidate.tab->renderer_handle; | 91 << ", is_focused " << candidate.app()->is_focused() |
| 92 << ", lastActivityTime " << candidate.app()->last_activity_time(); | |
| 93 } else if (candidate.tab()) { | |
| 94 out << "tab " << candidate.tab()->renderer_handle; | |
| 151 } | 95 } |
| 152 out << " with priority " << candidate.priority; | 96 out << ", process_type " << candidate.process_type(); |
| 153 return out; | 97 return out; |
| 154 } | 98 } |
| 155 | 99 |
| 156 // Holds the info of a newly focused tab or app window. The focused process is | 100 // Holds the info of a newly focused tab or app window. The focused process is |
| 157 // set to highest priority (lowest OOM score), but not immediately. To avoid | 101 // set to highest priority (lowest OOM score), but not immediately. To avoid |
| 158 // redundant settings the OOM score adjusting only happens after a timeout. If | 102 // redundant settings the OOM score adjusting only happens after a timeout. If |
| 159 // the process loses focus before the timeout, the adjustment is canceled. | 103 // the process loses focus before the timeout, the adjustment is canceled. |
| 160 // | 104 // |
| 161 // This information might be set on UI thread and looked up on FILE thread. So a | 105 // This information might be set on UI thread and looked up on FILE thread. So a |
| 162 // lock is needed to avoid racing. | 106 // lock is needed to avoid racing. |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 } | 342 } |
| 399 VLOG(2) << "Disable LowMemoryKiller"; | 343 VLOG(2) << "Disable LowMemoryKiller"; |
| 400 arc_process_instance_->DisableLowMemoryKiller(); | 344 arc_process_instance_->DisableLowMemoryKiller(); |
| 401 } | 345 } |
| 402 | 346 |
| 403 void TabManagerDelegate::OnInstanceClosed() { | 347 void TabManagerDelegate::OnInstanceClosed() { |
| 404 arc_process_instance_ = nullptr; | 348 arc_process_instance_ = nullptr; |
| 405 arc_process_instance_version_ = 0; | 349 arc_process_instance_version_ = 0; |
| 406 } | 350 } |
| 407 | 351 |
| 352 // TODO(cylee): Remove this function if Android process OOM score settings | |
| 353 // is moved back to Android. | |
| 354 // For example, negotiate non-overlapping OOM score ranges so Chrome and Android | |
| 355 // can set OOM score for processes in their own world. | |
| 408 void TabManagerDelegate::OnWindowActivated( | 356 void TabManagerDelegate::OnWindowActivated( |
| 409 aura::client::ActivationChangeObserver::ActivationReason reason, | 357 aura::client::ActivationChangeObserver::ActivationReason reason, |
| 410 aura::Window* gained_active, | 358 aura::Window* gained_active, |
| 411 aura::Window* lost_active) { | 359 aura::Window* lost_active) { |
| 412 if (IsArcWindow(gained_active)) { | 360 if (IsArcWindow(gained_active)) { |
| 413 // Currently there is no way to know which app is displayed in the ARC | 361 // Currently there is no way to know which app is displayed in the ARC |
| 414 // window, so schedule an early adjustment for all processes to reflect | 362 // window, so schedule an early adjustment for all processes to reflect |
| 415 // the change. | 363 // the change. |
| 416 // Put a dummy FocusedProcess with nspid = kInvalidArcAppNspid for now to | 364 // Put a dummy FocusedProcess with nspid = kInvalidArcAppNspid for now to |
| 417 // indicate the focused process is an arc app. | 365 // indicate the focused process is an arc app. |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 591 base::Bind(&TabManagerDelegate::AdjustOomPrioritiesImpl, | 539 base::Bind(&TabManagerDelegate::AdjustOomPrioritiesImpl, |
| 592 weak_ptr_factory_.GetWeakPtr(), tab_list))) { | 540 weak_ptr_factory_.GetWeakPtr(), tab_list))) { |
| 593 return; | 541 return; |
| 594 } | 542 } |
| 595 } | 543 } |
| 596 // Pass in a dummy list if unable to get ARC processes. | 544 // Pass in a dummy list if unable to get ARC processes. |
| 597 AdjustOomPrioritiesImpl(tab_list, std::vector<arc::ArcProcess>()); | 545 AdjustOomPrioritiesImpl(tab_list, std::vector<arc::ArcProcess>()); |
| 598 } | 546 } |
| 599 | 547 |
| 600 // Excludes persistent ARC apps, but still preserves active chrome tabs and | 548 // Excludes persistent ARC apps, but still preserves active chrome tabs and |
| 601 // top ARC apps. The latter ones should not be killed by TabManager since | 549 // focused ARC apps. The latter ones should not be killed by TabManager here, |
| 602 // we still want to adjust their oom_score_adj. | 550 // but we want to adjust their oom_score_adj. |
| 603 // static | 551 // static |
| 604 std::vector<TabManagerDelegate::Candidate> | 552 std::vector<TabManagerDelegate::Candidate> |
| 605 TabManagerDelegate::GetSortedCandidates( | 553 TabManagerDelegate::GetSortedCandidates( |
| 606 const TabStatsList& tab_list, | 554 const TabStatsList& tab_list, |
| 607 const std::vector<arc::ArcProcess>& arc_processes) { | 555 const std::vector<arc::ArcProcess>& arc_processes) { |
| 608 | 556 |
| 557 static const char kAppLauncherProcessName[] = "org.chromium.arc.applauncher"; | |
|
Yusuke Sato
2016/07/15 00:55:05
constexpr
cylee1
2016/07/15 17:31:13
Done.
| |
| 558 | |
| 609 std::vector<Candidate> candidates; | 559 std::vector<Candidate> candidates; |
| 610 candidates.reserve(tab_list.size() + arc_processes.size()); | 560 candidates.reserve(tab_list.size() + arc_processes.size()); |
| 611 | 561 |
| 612 for (const auto& tab : tab_list) { | 562 for (const auto& tab : tab_list) { |
| 613 candidates.push_back(Candidate(&tab, TabStatsToPriority(tab))); | 563 candidates.push_back(Candidate(&tab)); |
| 614 } | 564 } |
| 615 | 565 |
| 616 for (const auto& app : arc_processes) { | 566 for (const auto& app : arc_processes) { |
| 617 Candidate candidate(&app, AppStateToPriority(app.process_state())); | 567 // Skip persistent android processes since they should never be killed here. |
| 618 // Skip persistent android processes since we should never kill them. | 568 // Neither do we set their OOM scores so their score remains minimum. |
| 619 // Also don't ajust OOM score so their score remains min oom_score_adj. | 569 // |
| 620 if (candidate.priority >= ProcessPriority::ANDROID_PERSISTENT) | 570 // AppLauncher is treated specially in ARC++. For example it is taken |
| 571 // as the dummy foreground app from Android's point of view when the focused | |
| 572 // window is not an Android app. We prefer never kill it. | |
| 573 if (app.process_state() <= arc::mojom::ProcessState::PERSISTENT_UI || | |
| 574 app.process_name() == kAppLauncherProcessName) | |
| 621 continue; | 575 continue; |
| 622 candidates.push_back(candidate); | 576 candidates.push_back(Candidate(&app)); |
| 623 } | 577 } |
| 624 | 578 |
| 625 // Sort candidates according to priority. | 579 // Sort candidates according to priority. |
| 626 // TODO(cylee): Missing LRU property. Fix it when apps has the information. | |
| 627 std::sort(candidates.begin(), candidates.end()); | 580 std::sort(candidates.begin(), candidates.end()); |
| 628 | 581 |
| 629 return candidates; | 582 return candidates; |
| 630 } | 583 } |
| 631 | 584 |
| 632 bool TabManagerDelegate::KillArcProcess(const int nspid) { | 585 bool TabManagerDelegate::KillArcProcess(const int nspid) { |
| 633 if (!arc_process_instance_) | 586 if (!arc_process_instance_) |
| 634 return false; | 587 return false; |
| 635 arc_process_instance_->KillProcess(nspid, "LowMemoryKill"); | 588 arc_process_instance_->KillProcess(nspid, "LowMemoryKill"); |
| 636 return true; | 589 return true; |
| 637 } | 590 } |
| 638 | 591 |
| 639 bool TabManagerDelegate::KillTab(int64_t tab_id) { | 592 bool TabManagerDelegate::KillTab(int64_t tab_id) { |
| 640 // Check |tab_manager_| is alive before taking tabs into consideration. | 593 // Check |tab_manager_| is alive before taking tabs into consideration. |
| 641 return tab_manager_ && | 594 return tab_manager_ && |
| 642 tab_manager_->CanDiscardTab(tab_id) && | 595 tab_manager_->CanDiscardTab(tab_id) && |
| 643 tab_manager_->DiscardTabById(tab_id); | 596 tab_manager_->DiscardTabById(tab_id); |
| 644 } | 597 } |
| 645 | 598 |
| 646 void TabManagerDelegate::LowMemoryKillImpl( | 599 void TabManagerDelegate::LowMemoryKillImpl( |
| 647 const TabStatsList& tab_list, | 600 const TabStatsList& tab_list, |
| 648 const std::vector<arc::ArcProcess>& arc_processes) { | 601 const std::vector<arc::ArcProcess>& arc_processes) { |
| 649 | 602 |
| 650 VLOG(2) << "LowMemoryKilleImpl"; | 603 VLOG(2) << "LowMemoryKilleImpl"; |
| 651 std::vector<TabManagerDelegate::Candidate> candidates = | 604 const std::vector<TabManagerDelegate::Candidate> candidates = |
| 652 GetSortedCandidates(tab_list, arc_processes); | 605 GetSortedCandidates(tab_list, arc_processes); |
| 653 | 606 |
| 654 int target_memory_to_free_kb = mem_stat_->TargetMemoryToFreeKB(); | 607 int target_memory_to_free_kb = mem_stat_->TargetMemoryToFreeKB(); |
| 655 for (const auto& entry : candidates) { | 608 // Kill processes until the estimated amount of freed memory is sufficient to |
| 609 // bring the system memory back to a normal level. | |
| 610 // The list is sorted by importance descendingly, so we go through the list | |
| 611 // backwards. | |
| 612 for (auto it = candidates.rbegin(); it != candidates.rend(); ++it) { | |
|
Yusuke Sato
2016/07/15 00:55:05
const auto& it
probably?
cylee1
2016/07/15 17:31:13
No it will make "it" const so I cant ++it
candidat
| |
| 656 VLOG(3) << "Target memory to free: " << target_memory_to_free_kb << " KB"; | 613 VLOG(3) << "Target memory to free: " << target_memory_to_free_kb << " KB"; |
| 657 // Never kill selected tab or Android foreground app, regardless whether | 614 // Never kill selected tab or Android foreground app, regardless whether |
| 658 // they're in the active window. Since the user experience would be bad. | 615 // they're in the active window. Since the user experience would be bad. |
| 659 if ((!entry.is_arc_app && | 616 ProcessType process_type = it->process_type(); |
| 660 entry.priority >= ProcessPriority::CHROME_SELECTED) || | 617 if (process_type == ProcessType::VISIBLE_APP || |
| 661 (entry.is_arc_app && | 618 process_type == ProcessType::FOCUSED_APP || |
| 662 entry.priority >= ProcessPriority::ANDROID_TOP_INACTIVE)) { | 619 process_type == ProcessType::FOCUSED_TAB) { |
| 663 VLOG(2) << "Skipped killing " << entry; | 620 VLOG(2) << "Skipped killing " << *it; |
| 664 continue; | 621 continue; |
| 665 } | 622 } |
| 666 if (entry.is_arc_app) { | 623 if (it->app()) { |
| 667 int estimated_memory_freed_kb = | 624 int estimated_memory_freed_kb = |
| 668 mem_stat_->EstimatedMemoryFreedKB(entry.app->pid()); | 625 mem_stat_->EstimatedMemoryFreedKB(it->app()->pid()); |
| 669 if (KillArcProcess(entry.app->nspid())) { | 626 if (KillArcProcess(it->app()->nspid())) { |
| 670 target_memory_to_free_kb -= estimated_memory_freed_kb; | 627 target_memory_to_free_kb -= estimated_memory_freed_kb; |
| 671 uma_->ReportKill(estimated_memory_freed_kb); | 628 uma_->ReportKill(estimated_memory_freed_kb); |
| 672 VLOG(2) << "Killed " << entry; | 629 VLOG(2) << "Killed " << *it; |
| 673 } | 630 } |
| 674 } else { | 631 } else { |
| 675 int64_t tab_id = entry.tab->tab_contents_id; | 632 int64_t tab_id = it->tab()->tab_contents_id; |
| 676 int estimated_memory_freed_kb = | 633 int estimated_memory_freed_kb = |
| 677 mem_stat_->EstimatedMemoryFreedKB(entry.tab->renderer_handle); | 634 mem_stat_->EstimatedMemoryFreedKB(it->tab()->renderer_handle); |
| 678 if (KillTab(tab_id)) { | 635 if (KillTab(tab_id)) { |
| 679 target_memory_to_free_kb -= estimated_memory_freed_kb; | 636 target_memory_to_free_kb -= estimated_memory_freed_kb; |
| 680 uma_->ReportKill(estimated_memory_freed_kb); | 637 uma_->ReportKill(estimated_memory_freed_kb); |
| 681 VLOG(2) << "Killed " << entry; | 638 VLOG(2) << "Killed " << *it; |
| 682 } | 639 } |
| 683 } | 640 } |
| 684 if (target_memory_to_free_kb < 0) | 641 if (target_memory_to_free_kb < 0) |
| 685 break; | 642 break; |
| 686 } | 643 } |
| 687 } | 644 } |
| 688 | 645 |
| 689 void TabManagerDelegate::AdjustOomPrioritiesImpl( | 646 void TabManagerDelegate::AdjustOomPrioritiesImpl( |
| 690 const TabStatsList& tab_list, | 647 const TabStatsList& tab_list, |
| 691 const std::vector<arc::ArcProcess>& arc_processes) { | 648 const std::vector<arc::ArcProcess>& arc_processes) { |
| 692 // Least important first. | 649 // Least important first. |
| 693 auto candidates = GetSortedCandidates(tab_list, arc_processes); | 650 const auto candidates = GetSortedCandidates(tab_list, arc_processes); |
| 694 | 651 |
| 695 // Now we assign priorities based on the sorted list. We're assigning | 652 // Now we assign priorities based on the sorted list. We're assigning |
| 696 // priorities in the range of kLowestRendererOomScore to | 653 // priorities in the range of kLowestRendererOomScore to |
| 697 // kHighestRendererOomScore (defined in chrome_constants.h). oom_score_adj | 654 // kHighestRendererOomScore (defined in chrome_constants.h). oom_score_adj |
| 698 // takes values from -1000 to 1000. Negative values are reserved for system | 655 // takes values from -1000 to 1000. Negative values are reserved for system |
| 699 // processes, and we want to give some room below the range we're using to | 656 // processes, and we want to give some room below the range we're using to |
| 700 // allow for things that want to be above the renderers in priority, so the | 657 // allow for things that want to be above the renderers in priority, so the |
| 701 // defined range gives us some variation in priority without taking up the | 658 // defined range gives us some variation in priority without taking up the |
| 702 // whole range. In the end, however, it's a pretty arbitrary range to use. | 659 // whole range. In the end, however, it's a pretty arbitrary range to use. |
| 703 // Higher values are more likely to be killed by the OOM killer. | 660 // Higher values are more likely to be killed by the OOM killer. |
| 704 | 661 |
| 705 // Break the processes into 2 parts. This is to help lower the chance of | 662 // Break the processes into 2 parts. This is to help lower the chance of |
| 706 // altering OOM score for many processes on any small change. | 663 // altering OOM score for many processes on any small change. |
| 707 int range_middle = | 664 int range_middle = |
| 708 (chrome::kLowestRendererOomScore + chrome::kHighestRendererOomScore) / 2; | 665 (chrome::kLowestRendererOomScore + chrome::kHighestRendererOomScore) / 2; |
| 709 | 666 |
| 710 // Find some pivot point. For now processes with priority >= CHROME_INTERNAL | 667 // Find some pivot point. For now processes with priority >= CHROME_INTERNAL |
| 711 // are prone to be affected by LRU change. Taking them as "high priority" | 668 // are prone to be affected by LRU change. Taking them as "high priority" |
| 712 // processes. | 669 // processes. |
| 713 auto lower_priority_part = candidates.rend(); | 670 auto lower_priority_part = candidates.end(); |
| 714 // Iterate in reverse order since the list is sorted by least importance. | 671 for (auto it = candidates.begin(); it != candidates.end(); ++it) { |
|
Yusuke Sato
2016/07/15 00:55:05
same
cylee1
2016/07/15 17:31:13
same.
| |
| 715 for (auto it = candidates.rbegin(); it != candidates.rend(); ++it) { | 672 if (it->process_type() >= ProcessType::BACKGROUND_APP) { |
| 716 if (it->priority < ProcessPriority::CHROME_INTERNAL) { | |
| 717 lower_priority_part = it; | 673 lower_priority_part = it; |
| 718 break; | 674 break; |
| 719 } | 675 } |
| 720 } | 676 } |
| 721 | 677 |
| 722 ProcessScoreMap new_map; | 678 ProcessScoreMap new_map; |
| 723 | 679 |
| 724 // Higher priority part. | 680 // Higher priority part. |
| 725 DistributeOomScoreInRange(candidates.rbegin(), lower_priority_part, | 681 DistributeOomScoreInRange(candidates.begin(), lower_priority_part, |
| 726 chrome::kLowestRendererOomScore, range_middle, | 682 chrome::kLowestRendererOomScore, range_middle, |
| 727 &new_map); | 683 &new_map); |
| 728 // Lower priority part. | 684 // Lower priority part. |
| 729 DistributeOomScoreInRange(lower_priority_part, candidates.rend(), | 685 DistributeOomScoreInRange(lower_priority_part, candidates.end(), |
| 730 range_middle, chrome::kHighestRendererOomScore, | 686 range_middle, chrome::kHighestRendererOomScore, |
| 731 &new_map); | 687 &new_map); |
| 732 base::AutoLock oom_score_autolock(oom_score_lock_); | 688 base::AutoLock oom_score_autolock(oom_score_lock_); |
| 733 oom_score_map_.swap(new_map); | 689 oom_score_map_.swap(new_map); |
| 734 } | 690 } |
| 735 | 691 |
| 736 void TabManagerDelegate::SetOomScoreAdjForApp(int nspid, int score) { | 692 void TabManagerDelegate::SetOomScoreAdjForApp(int nspid, int score) { |
| 737 if (!arc_process_instance_) | 693 if (!arc_process_instance_) |
| 738 return; | 694 return; |
| 739 if (arc_process_instance_version_ < 2) { | 695 if (arc_process_instance_version_ < 2) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 755 void TabManagerDelegate::SetOomScoreAdjForTabsOnFileThread( | 711 void TabManagerDelegate::SetOomScoreAdjForTabsOnFileThread( |
| 756 const std::vector<std::pair<base::ProcessHandle, int>>& entries) { | 712 const std::vector<std::pair<base::ProcessHandle, int>>& entries) { |
| 757 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 713 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 758 for (const auto& entry : entries) { | 714 for (const auto& entry : entries) { |
| 759 content::ZygoteHost::GetInstance()->AdjustRendererOOMScore(entry.first, | 715 content::ZygoteHost::GetInstance()->AdjustRendererOOMScore(entry.first, |
| 760 entry.second); | 716 entry.second); |
| 761 } | 717 } |
| 762 } | 718 } |
| 763 | 719 |
| 764 void TabManagerDelegate::DistributeOomScoreInRange( | 720 void TabManagerDelegate::DistributeOomScoreInRange( |
| 765 std::vector<TabManagerDelegate::Candidate>::reverse_iterator rbegin, | 721 std::vector<TabManagerDelegate::Candidate>::const_iterator begin, |
| 766 std::vector<TabManagerDelegate::Candidate>::reverse_iterator rend, | 722 std::vector<TabManagerDelegate::Candidate>::const_iterator end, |
| 767 int range_begin, | 723 int range_begin, |
| 768 int range_end, | 724 int range_end, |
| 769 ProcessScoreMap* new_map) { | 725 ProcessScoreMap* new_map) { |
| 770 // OOM score setting for tabs involves file system operation so should be | 726 // OOM score setting for tabs involves file system operation so should be |
| 771 // done on file thread. | 727 // done on file thread. |
| 772 std::vector<std::pair<base::ProcessHandle, int>> oom_score_for_tabs; | 728 std::vector<std::pair<base::ProcessHandle, int>> oom_score_for_tabs; |
| 773 | 729 |
| 774 // Though there might be duplicate process handles, it doesn't matter to | 730 // Though there might be duplicate process handles, it doesn't matter to |
| 775 // overestimate the number of processes here since the we don't need to | 731 // overestimate the number of processes here since the we don't need to |
| 776 // use up the full range. | 732 // use up the full range. |
| 777 int num = (rend - rbegin); | 733 int num = (end - begin); |
| 778 const float priority_increment = | 734 const float priority_increment = |
| 779 static_cast<float>(range_end - range_begin) / num; | 735 static_cast<float>(range_end - range_begin) / num; |
| 780 | 736 |
| 781 float priority = range_begin; | 737 float priority = range_begin; |
| 782 for (auto cur = rbegin; cur != rend; ++cur) { | 738 for (auto cur = begin; cur != end; ++cur) { |
| 783 int score = static_cast<int>(priority + 0.5f); | 739 int score = static_cast<int>(priority + 0.5f); |
| 784 if (cur->is_arc_app) { | 740 if (cur->app()) { |
| 785 // Use pid as map keys so it's globally unique. | 741 // Use pid as map keys so it's globally unique. |
| 786 (*new_map)[cur->app->pid()] = score; | 742 (*new_map)[cur->app()->pid()] = score; |
| 787 int cur_app_pid_score = 0; | 743 int cur_app_pid_score = 0; |
| 788 { | 744 { |
| 789 base::AutoLock oom_score_autolock(oom_score_lock_); | 745 base::AutoLock oom_score_autolock(oom_score_lock_); |
| 790 cur_app_pid_score = oom_score_map_[cur->app->pid()]; | 746 cur_app_pid_score = oom_score_map_[cur->app()->pid()]; |
| 791 } | 747 } |
| 792 if (cur_app_pid_score != score) { | 748 if (cur_app_pid_score != score) { |
| 793 VLOG(3) << "Set OOM score " << score << " for " << *cur; | 749 VLOG(3) << "Set OOM score " << score << " for " << *cur; |
| 794 SetOomScoreAdjForApp(cur->app->nspid(), score); | 750 SetOomScoreAdjForApp(cur->app()->nspid(), score); |
| 795 } | 751 } |
| 796 } else { | 752 } else { |
| 797 base::ProcessHandle process_handle = cur->tab->renderer_handle; | 753 base::ProcessHandle process_handle = cur->tab()->renderer_handle; |
| 798 // 1. tab_list contains entries for already-discarded tabs. If the PID | 754 // 1. tab_list contains entries for already-discarded tabs. If the PID |
| 799 // (renderer_handle) is zero, we don't need to adjust the oom_score. | 755 // (renderer_handle) is zero, we don't need to adjust the oom_score. |
| 800 // 2. Only add unseen process handle so if there's multiple tab maps to | 756 // 2. Only add unseen process handle so if there's multiple tab maps to |
| 801 // the same process, the process is set to an OOM score based on its "most | 757 // the same process, the process is set to an OOM score based on its "most |
| 802 // important" tab. | 758 // important" tab. |
| 803 if (process_handle != 0 && | 759 if (process_handle != 0 && |
| 804 new_map->find(process_handle) == new_map->end()) { | 760 new_map->find(process_handle) == new_map->end()) { |
| 805 (*new_map)[process_handle] = score; | 761 (*new_map)[process_handle] = score; |
| 806 int process_handle_score = 0; | 762 int process_handle_score = 0; |
| 807 { | 763 { |
| 808 base::AutoLock oom_score_autolock(oom_score_lock_); | 764 base::AutoLock oom_score_autolock(oom_score_lock_); |
| 809 process_handle_score = oom_score_map_[process_handle]; | 765 process_handle_score = oom_score_map_[process_handle]; |
| 810 } | 766 } |
| 811 if (process_handle_score != score) { | 767 if (process_handle_score != score) { |
| 812 oom_score_for_tabs.push_back(std::make_pair(process_handle, score)); | 768 oom_score_for_tabs.push_back(std::make_pair(process_handle, score)); |
| 813 VLOG(3) << "Set OOM score " << score << " for " << *cur; | 769 VLOG(3) << "Set OOM score " << score << " for " << *cur; |
| 814 } | 770 } |
| 815 } else { | 771 } else { |
| 816 continue; // Skip priority increment. | 772 continue; // Skip priority increment. |
| 817 } | 773 } |
| 818 } | 774 } |
| 819 priority += priority_increment; | 775 priority += priority_increment; |
| 820 } | 776 } |
| 821 | 777 |
| 822 if (oom_score_for_tabs.size()) | 778 if (oom_score_for_tabs.size()) |
| 823 SetOomScoreAdjForTabs(oom_score_for_tabs); | 779 SetOomScoreAdjForTabs(oom_score_for_tabs); |
| 824 } | 780 } |
| 825 | 781 |
| 826 } // namespace memory | 782 } // namespace memory |
| OLD | NEW |