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
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/controller/oom_intervention_impl.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "base/debug/crash_logging.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/single_thread_task_runner.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_gc_for_context_dispose.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/loader/frame_load_request.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread_scheduler.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
namespace blink {
namespace {
base::debug::CrashKeyString* GetStateCrashKey() {
static auto* crash_key = base::debug::AllocateCrashKeyString(
"oom_intervention_state", base::debug::CrashKeySize::Size32);
return crash_key;
}
void NavigateLocalAdsFrames(LocalFrame* frame) {
// This navigates all the frames detected as an advertisement to about:blank.
DCHECK(frame);
for (Frame* child = frame->Tree().FirstChild(); child;
child = child->Tree().TraverseNext(frame)) {
if (auto* child_local_frame = DynamicTo<LocalFrame>(child)) {
if (child_local_frame->IsAdFrame()) {
FrameLoadRequest request(frame->DomWindow(),
ResourceRequest(BlankURL()));
child_local_frame->Navigate(request, WebFrameLoadType::kStandard);
}
}
// TODO(yuzus): Once AdsTracker for remote frames is implemented and OOPIF
// is enabled on low-end devices, navigate remote ads as well.
}
}
} // namespace
// static
void OomInterventionImpl::BindReceiver(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
mojo::PendingReceiver<mojom::blink::OomIntervention> receiver) {
mojo::MakeSelfOwnedReceiver(
std::make_unique<OomInterventionImpl>(
base::PassKey<OomInterventionImpl>(), task_runner),
std::move(receiver), task_runner);
}
OomInterventionImpl::OomInterventionImpl(
base::PassKey<OomInterventionImpl> pass_key,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: OomInterventionImpl(std::move(task_runner)) {}
OomInterventionImpl::OomInterventionImpl(
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: task_runner_(std::move(task_runner)) {
static bool initial_crash_key_set = false;
if (!initial_crash_key_set) {
initial_crash_key_set = true;
base::debug::SetCrashKeyString(GetStateCrashKey(), "before");
}
}
OomInterventionImpl::~OomInterventionImpl() {
MemoryUsageMonitorInstance().RemoveObserver(this);
}
void OomInterventionImpl::StartDetection(
mojo::PendingRemote<mojom::blink::OomInterventionHost> host,
mojom::blink::DetectionArgsPtr detection_args,
bool renderer_pause_enabled,
bool navigate_ads_enabled,
bool purge_v8_memory_enabled) {
host_.Bind(std::move(host));
detection_args_ = std::move(detection_args);
renderer_pause_enabled_ = renderer_pause_enabled;
navigate_ads_enabled_ = navigate_ads_enabled;
purge_v8_memory_enabled_ = purge_v8_memory_enabled;
MemoryUsageMonitorInstance().AddObserver(this);
}
MemoryUsageMonitor& OomInterventionImpl::MemoryUsageMonitorInstance() {
return MemoryUsageMonitor::Instance();
}
void OomInterventionImpl::OnMemoryPing(MemoryUsage usage) {
// Ignore pings without process memory usage information.
if (std::isnan(usage.private_footprint_bytes) ||
std::isnan(usage.swap_bytes) || std::isnan(usage.vm_size_bytes))
return;
Check(usage);
}
void OomInterventionImpl::Check(MemoryUsage usage) {
DCHECK(host_);
bool oom_detected = false;
oom_detected |= detection_args_->private_footprint_threshold > 0 &&
usage.private_footprint_bytes >
detection_args_->private_footprint_threshold;
if (oom_detected) {
base::debug::SetCrashKeyString(GetStateCrashKey(), "during");
if (navigate_ads_enabled_ || purge_v8_memory_enabled_) {
for (const auto& page : Page::OrdinaryPages()) {
for (Frame* frame = page->MainFrame(); frame;
frame = frame->Tree().TraverseNext()) {
auto* local_frame = DynamicTo<LocalFrame>(frame);
if (!local_frame)
continue;
if (navigate_ads_enabled_)
NavigateLocalAdsFrames(local_frame);
if (purge_v8_memory_enabled_)
local_frame->ForciblyPurgeV8Memory();
}
}
}
if (renderer_pause_enabled_) {
// The ScopedPagePauser is destroyed when the intervention is declined and
// mojo strong binding is disconnected.
pauser_ = std::make_unique<ScopedPagePauser>();
}
host_->OnHighMemoryUsage();
MemoryUsageMonitorInstance().RemoveObserver(this);
// Send memory pressure notification to trigger GC.
task_runner_->PostTask(FROM_HERE, WTF::BindOnce(&TriggerGC));
// Notify V8GCForContextDispose that page navigation gc is needed when
// intervention runs, as it indicates that memory usage is high.
V8GCForContextDispose::Instance().SetForcePageNavigationGC();
}
}
void OomInterventionImpl::TriggerGC() {
Thread::MainThread()
->Scheduler()
->ToMainThreadScheduler()
->ForEachMainThreadIsolate(WTF::BindRepeating([](v8::Isolate* isolate) {
isolate->MemoryPressureNotification(v8::MemoryPressureLevel::kCritical);
}));
}
} // namespace blink
|