1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/performance_manager/policies/page_discarding_helper.h"
#include <cstdint>
#include <memory>
#include <utility>
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "build/build_config.h"
#include "chrome/browser/performance_manager/policies/policy_features.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/public/decorators/tab_page_decorator.h"
#include "components/performance_manager/public/graph/frame_node.h"
#include "components/performance_manager/public/graph/graph_operations.h"
#include "components/performance_manager/public/graph/node_attached_data.h"
#include "components/performance_manager/public/graph/node_data_describer_registry.h"
#include "components/performance_manager/public/graph/node_data_describer_util.h"
#include "components/performance_manager/public/graph/process_node.h"
#include "components/performance_manager/public/user_tuning/tab_revisit_tracker.h"
using performance_manager::mechanism::PageDiscarder;
namespace performance_manager::policies {
namespace {
BASE_FEATURE(kSkipDiscardsDrivenByStaleSignal,
"SkipDiscardDrivenByStaleSignal",
base::FEATURE_DISABLED_BY_DEFAULT);
const char kDescriberName[] = "PageDiscardingHelper";
#if BUILDFLAG(IS_CHROMEOS)
// A 25% compression ratio is very conservative, and it matches the
// value used by resourced when calculating available memory.
static const uint64_t kSwapFootprintDiscount = 4;
#endif
using NodeFootprintMap = base::flat_map<const PageNode*, uint64_t>;
// Returns the mapping from page_node to its memory footprint estimation.
NodeFootprintMap GetPageNodeFootprintEstimateKb(
const std::vector<PageNodeSortProxy>& candidates) {
// Initialize the result map in one shot for time complexity O(n * log(n)).
NodeFootprintMap::container_type result_container;
result_container.reserve(candidates.size());
for (const auto& candidate : candidates) {
result_container.emplace_back(candidate.page_node().get(), 0);
}
NodeFootprintMap result(std::move(result_container));
// TODO(crbug.com/40194476): Use visitor to accumulate the result to avoid
// allocating extra lists of frame nodes behind the scenes.
// List all the processes associated with these page nodes.
base::flat_set<const ProcessNode*> process_nodes;
for (const auto& candidate : candidates) {
base::flat_set<const ProcessNode*> processes =
GraphOperations::GetAssociatedProcessNodes(candidate.page_node().get());
process_nodes.insert(processes.begin(), processes.end());
}
// Compute the resident set of each page by simply summing up the estimated
// resident set of all its frames.
for (const ProcessNode* process_node : process_nodes) {
ProcessNode::NodeSetView<const FrameNode*> process_frames =
process_node->GetFrameNodes();
if (!process_frames.size()) {
continue;
}
// Get the footprint of the process and split it equally across its
// frames.
uint64_t footprint_kb = process_node->GetResidentSetKb();
#if BUILDFLAG(IS_CHROMEOS)
footprint_kb += process_node->GetPrivateSwapKb() / kSwapFootprintDiscount;
#endif
footprint_kb /= process_frames.size();
for (const FrameNode* frame_node : process_frames) {
// Check if the frame belongs to a discardable page, if so update the
// resident set of the page.
auto iter = result.find(frame_node->GetPageNode());
if (iter == result.end()) {
continue;
}
iter->second += footprint_kb;
}
}
return result;
}
void RecordDiscardedTabMetrics(const PageNodeSortProxy& candidate) {
// Logs a histogram entry to track the proportion of discarded tabs that
// were protected at the time of discard.
UMA_HISTOGRAM_BOOLEAN("Discarding.DiscardingProtectedTab2",
candidate.is_protected());
// Logs a histogram entry to track the proportion of discarded tabs that
// were focused at the time of discard.
UMA_HISTOGRAM_BOOLEAN("Discarding.DiscardingFocusedTab2",
candidate.is_focused());
}
} // namespace
PageDiscardingHelper::PageDiscardingHelper()
: page_discarder_(std::make_unique<PageDiscarder>()) {}
PageDiscardingHelper::~PageDiscardingHelper() = default;
std::optional<base::TimeTicks> PageDiscardingHelper::DiscardAPage(
DiscardEligibilityPolicy::DiscardReason discard_reason,
base::TimeDelta minimum_time_in_background) {
return DiscardMultiplePages(std::nullopt, false, discard_reason,
minimum_time_in_background);
}
std::optional<base::TimeTicks> PageDiscardingHelper::DiscardMultiplePages(
std::optional<memory_pressure::ReclaimTarget> reclaim_target,
bool discard_protected_tabs,
DiscardEligibilityPolicy::DiscardReason discard_reason,
base::TimeDelta minimum_time_in_background) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (reclaim_target) {
if (base::FeatureList::IsEnabled(kSkipDiscardsDrivenByStaleSignal)) {
reclaim_target =
unnecessary_discard_monitor_.CorrectReclaimTarget(*reclaim_target);
}
unnecessary_discard_monitor_.OnReclaimTargetBegin(*reclaim_target);
}
LOG(WARNING) << "Discarding multiple pages with target (kb): "
<< (reclaim_target ? reclaim_target->target_kb : 0)
<< ", discard_protected_tabs: " << discard_protected_tabs;
DiscardEligibilityPolicy* eligiblity_policy =
DiscardEligibilityPolicy::GetFromGraph(GetOwningGraph());
DCHECK(eligiblity_policy);
std::vector<PageNodeSortProxy> candidates;
for (const PageNode* page_node : GetOwningGraph()->GetAllPageNodes()) {
CanDiscardResult can_discard_result = eligiblity_policy->CanDiscard(
page_node, discard_reason, minimum_time_in_background);
if (can_discard_result == CanDiscardResult::kDisallowed) {
continue;
}
if (can_discard_result == CanDiscardResult::kProtected &&
!discard_protected_tabs) {
continue;
}
candidates.emplace_back(page_node->GetWeakPtr(), can_discard_result,
page_node->IsVisible(), page_node->IsFocused(),
page_node->GetLastVisibilityChangeTime());
}
// Sorts with descending importance.
std::sort(candidates.rbegin(), candidates.rend());
UMA_HISTOGRAM_COUNTS_100("Discarding.DiscardCandidatesCount",
candidates.size());
// Estimate the memory footprint of each candidate to determine when enough
// candidates have been discarded to reach the `reclaim_target`. This is not
// needed when there is no `reclaim_target`.
NodeFootprintMap page_node_footprint_kb;
if (reclaim_target) {
// Only compute the estimated memory footprint if needed.
page_node_footprint_kb = GetPageNodeFootprintEstimateKb(candidates);
}
uint64_t total_reclaim_kb = 0;
std::optional<base::TimeTicks> first_successful_discard_time;
// Note: If `reclaim_target->target_kb` is zero, this loop is not entered.
while (!candidates.empty() &&
(!reclaim_target || total_reclaim_kb < reclaim_target->target_kb)) {
const PageNodeSortProxy candidate = std::move(candidates.back());
candidates.pop_back();
if (!candidate.page_node()) {
// Skip if discarding another page caused this page to be deleted.
continue;
}
const PageNode* node = candidate.page_node().get();
std::optional<uint64_t> node_reclaim_kb;
if (reclaim_target) {
// TODO(crbug.com/40755583): Use the `estimated_memory_freed_kb` obtained
// from `DiscardPageNode()` below to avoid the need to build
// `page_node_footprint_kb`.
// The node footprint value is updated by ProcessMetricsDecorator
// periodically. The footprint value is 0 for nodes that have never been
// updated, estimate the RSS value to 80 MiB for these nodes. 80 MiB is
// the average Memory.Renderer.PrivateMemoryFootprint histogram value on
// Windows in August 2021.
node_reclaim_kb = (page_node_footprint_kb[node])
? page_node_footprint_kb[node]
: 80 * 1024;
LOG(WARNING) << "Queueing discard attempt, type="
<< performance_manager::PageNode::ToString(node->GetType())
<< ", flags=[" << (candidate.is_focused() ? " focused" : "")
<< (candidate.is_protected() ? " protected" : "")
<< (candidate.is_visible() ? " visible" : "")
<< " ] to save " << node_reclaim_kb.value() << " KiB";
}
// Adorn the PageNode with a discard attempt marker to make sure that we
// don't try to discard it multiple times if it fails to be discarded. In
// practice this should only happen to prerenderers.
DiscardEligibilityPolicy::AddDiscardAttemptMarker(
PageNodeImpl::FromNode(node));
// Do the discard.
std::optional<uint64_t> estimated_memory_freed_kb =
page_discarder_->DiscardPageNode(node, discard_reason);
// If discard is successful:
if (estimated_memory_freed_kb.has_value()) {
const base::TimeTicks discard_time = base::TimeTicks::Now();
unnecessary_discard_monitor_.OnDiscard(estimated_memory_freed_kb.value(),
discard_time);
RecordDiscardedTabMetrics(candidate);
// Without a reclaim target: Return after the first successful discard.
if (!reclaim_target) {
return discard_time;
}
// With a reclaim target: Update the amount of memory reclaimed and the
// time of the first successful discard, and loop again.
total_reclaim_kb += node_reclaim_kb.value();
if (!first_successful_discard_time.has_value()) {
first_successful_discard_time = discard_time;
}
}
}
unnecessary_discard_monitor_.OnReclaimTargetEnd();
return first_successful_discard_time;
}
bool PageDiscardingHelper::ImmediatelyDiscardMultiplePages(
const std::vector<const PageNode*>& page_nodes,
DiscardEligibilityPolicy::DiscardReason discard_reason,
base::TimeDelta minimum_time_in_background) {
DiscardEligibilityPolicy* eligiblity_policy =
DiscardEligibilityPolicy::GetFromGraph(GetOwningGraph());
DCHECK(eligiblity_policy);
std::vector<base::WeakPtr<const PageNode>> eligible_nodes;
for (const PageNode* node : page_nodes) {
if (eligiblity_policy->CanDiscard(node, discard_reason,
minimum_time_in_background) ==
CanDiscardResult::kEligible) {
eligible_nodes.emplace_back(node->GetWeakPtr());
}
}
bool had_successful_discard = false;
for (base::WeakPtr<const PageNode> node : eligible_nodes) {
// Skip if discarding another page caused this page to be deleted.
if (!node) {
continue;
}
had_successful_discard |=
page_discarder_->DiscardPageNode(node.get(), discard_reason)
.has_value();
}
return had_successful_discard;
}
void PageDiscardingHelper::SetMockDiscarderForTesting(
std::unique_ptr<PageDiscarder> discarder) {
page_discarder_ = std::move(discarder);
}
void PageDiscardingHelper::OnPassedToGraph(Graph* graph) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
graph->AddPageNodeObserver(this);
graph->GetNodeDataDescriberRegistry()->RegisterDescriber(this,
kDescriberName);
}
void PageDiscardingHelper::OnTakenFromGraph(Graph* graph) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
graph->GetNodeDataDescriberRegistry()->UnregisterDescriber(this);
graph->RemovePageNodeObserver(this);
}
base::Value::Dict PageDiscardingHelper::DescribePageNodeData(
const PageNode* node) const {
base::Value::Dict ret;
TabPageDecorator::TabHandle* tab_handle =
TabPageDecorator::FromPageNode(node);
if (tab_handle) {
TabRevisitTracker* revisit_tracker =
GetOwningGraph()->GetRegisteredObjectAs<TabRevisitTracker>();
CHECK(revisit_tracker);
TabRevisitTracker::StateBundle state =
revisit_tracker->GetStateForTabHandle(tab_handle);
ret.Set("num_revisits", static_cast<int>(state.num_revisits));
}
return ret;
}
} // namespace performance_manager::policies
|