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 321 322 323
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/heap_profiling/multi_process/client_connection_manager.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/no_destructor.h"
#include "base/rand_util.h"
#include "components/services/heap_profiling/public/cpp/controller.h"
#include "components/services/heap_profiling/public/cpp/profiling_client.h"
#include "components/services/heap_profiling/public/cpp/settings.h"
#include "components/services/heap_profiling/public/mojom/heap_profiling_client.mojom.h"
#include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h"
#include "content/public/browser/browser_child_process_host.h"
#include "content/public/browser/browser_child_process_host_iterator.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_host.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/process_type.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace heap_profiling {
namespace {
bool ShouldProfileNonRendererProcessType(Mode mode, int process_type) {
switch (mode) {
case Mode::kAll:
return true;
case Mode::kAllRenderers:
// Renderer logic is handled in ClientConnectionManager::Observe.
return false;
case Mode::kManual:
return false;
case Mode::kMinimal:
return (process_type == content::ProcessType::PROCESS_TYPE_GPU ||
process_type == content::ProcessType::PROCESS_TYPE_BROWSER);
case Mode::kGpu:
return process_type == content::ProcessType::PROCESS_TYPE_GPU;
case Mode::kBrowser:
return process_type == content::ProcessType::PROCESS_TYPE_BROWSER;
case Mode::kRendererSampling:
// Renderer logic is handled in ClientConnectionManager::Observe.
return false;
case Mode::kUtilitySampling:
// Sample each utility process with 1/3 probability.
if (process_type == content::ProcessType::PROCESS_TYPE_UTILITY)
return (base::RandUint64() % 3) < 1;
return false;
case Mode::kUtilityAndBrowser:
return process_type == content::ProcessType::PROCESS_TYPE_UTILITY ||
process_type == content::ProcessType::PROCESS_TYPE_BROWSER;
case Mode::kNone:
return false;
case Mode::kCount:
// Fall through to hit NOTREACHED() below.
{}
}
NOTREACHED();
return false;
}
void StartProfilingClientOnIOThread(
base::WeakPtr<Controller> controller,
mojo::PendingRemote<mojom::ProfilingClient> client,
base::ProcessId pid,
mojom::ProcessType process_type,
base::OnceClosure started_profiling_closure) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
if (!controller)
return;
controller->StartProfilingClient(std::move(client), pid, process_type,
std::move(started_profiling_closure));
}
void StartProfilingBrowserProcessOnIOThread(
base::WeakPtr<Controller> controller,
base::OnceClosure started_profiling_closure) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
if (!controller)
return;
static base::NoDestructor<ProfilingClient> client;
mojo::PendingRemote<mojom::ProfilingClient> remote;
client->BindToInterface(remote.InitWithNewPipeAndPassReceiver());
controller->StartProfilingClient(std::move(remote), base::GetCurrentProcId(),
mojom::ProcessType::BROWSER,
std::move(started_profiling_closure));
}
} // namespace
ClientConnectionManager::ClientConnectionManager(
base::WeakPtr<Controller> controller,
Mode mode)
: controller_(controller), mode_(mode) {}
ClientConnectionManager::~ClientConnectionManager() {
Remove(this);
}
void ClientConnectionManager::Start() {
Add(this);
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::NotificationService::AllBrowserContextsAndSources());
StartProfilingExistingProcessesIfNecessary();
}
Mode ClientConnectionManager::GetMode() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
return mode_;
}
void ClientConnectionManager::StartProfilingProcess(
base::ProcessId pid,
base::OnceClosure started_profiling_closure) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
mode_ = Mode::kManual;
// The RenderProcessHost iterator must be used on the UI thread.
for (auto iter = content::RenderProcessHost::AllHostsIterator();
!iter.IsAtEnd(); iter.Advance()) {
if (pid == iter.GetCurrentValue()->GetProcess().Pid()) {
StartProfilingRenderer(iter.GetCurrentValue(),
std::move(started_profiling_closure));
return;
}
}
// Check if the request is for the current process.
if (pid == base::GetCurrentProcId()) {
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&StartProfilingBrowserProcessOnIOThread, controller_,
std::move(started_profiling_closure)));
return;
}
// Check if the request is for a non-renderer child process.
for (content::BrowserChildProcessHostIterator browser_child_iter;
!browser_child_iter.Done(); ++browser_child_iter) {
const content::ChildProcessData& data = browser_child_iter.GetData();
if (data.GetProcess().Pid() == pid) {
StartProfilingNonRendererChild(data,
std::move(started_profiling_closure));
return;
}
}
DLOG(WARNING)
<< "Attempt to start profiling failed as no process was found with pid: "
<< pid;
}
bool ClientConnectionManager::AllowedToProfileRenderer(
content::RenderProcessHost* host) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
return true;
}
void ClientConnectionManager::SetModeForTesting(Mode mode) {
mode_ = mode;
}
void ClientConnectionManager::StartProfilingExistingProcessesIfNecessary() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// Start profiling the current process.
if (ShouldProfileNonRendererProcessType(
mode_, content::ProcessType::PROCESS_TYPE_BROWSER)) {
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&StartProfilingBrowserProcessOnIOThread,
controller_, base::DoNothing()));
}
// Start profiling connected renderers.
for (auto iter = content::RenderProcessHost::AllHostsIterator();
!iter.IsAtEnd(); iter.Advance()) {
if (ShouldProfileNewRenderer(iter.GetCurrentValue()) &&
iter.GetCurrentValue()->GetProcess().Handle() !=
base::kNullProcessHandle) {
StartProfilingRenderer(iter.GetCurrentValue());
}
}
for (content::BrowserChildProcessHostIterator browser_child_iter;
!browser_child_iter.Done(); ++browser_child_iter) {
const content::ChildProcessData& data = browser_child_iter.GetData();
if (ShouldProfileNonRendererProcessType(mode_, data.process_type) &&
data.GetProcess().IsValid()) {
StartProfilingNonRendererChild(data);
}
}
}
void ClientConnectionManager::BrowserChildProcessLaunchedAndConnected(
const content::ChildProcessData& data) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// Ensure this is only called for all non-renderer browser child processes
// so as not to collide with logic in ClientConnectionManager::Observe().
DCHECK_NE(data.process_type, content::ProcessType::PROCESS_TYPE_RENDERER);
if (!ShouldProfileNonRendererProcessType(mode_, data.process_type))
return;
StartProfilingNonRendererChild(data);
}
void ClientConnectionManager::StartProfilingNonRendererChild(
const content::ChildProcessData& data,
base::OnceClosure started_profiling_closure) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
content::BrowserChildProcessHost* host =
content::BrowserChildProcessHost::FromID(data.id);
if (!host)
return;
mojom::ProcessType process_type =
(data.process_type == content::ProcessType::PROCESS_TYPE_GPU)
? mojom::ProcessType::GPU
: mojom::ProcessType::OTHER;
// Tell the child process to start profiling.
mojo::PendingRemote<mojom::ProfilingClient> client;
host->GetHost()->BindReceiver(client.InitWithNewPipeAndPassReceiver());
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&StartProfilingClientOnIOThread, controller_,
std::move(client), data.GetProcess().Pid(), process_type,
std::move(started_profiling_closure)));
}
void ClientConnectionManager::OnRenderProcessHostCreated(
content::RenderProcessHost* host) {
if (ShouldProfileNewRenderer(host)) {
StartProfilingRenderer(host);
}
}
void ClientConnectionManager::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
content::RenderProcessHost* host =
content::Source<content::RenderProcessHost>(source).ptr();
// NOTIFICATION_RENDERER_PROCESS_CLOSED corresponds to death of an underlying
// RenderProcess. NOTIFICATION_RENDERER_PROCESS_TERMINATED corresponds to when
// the RenderProcessHost's lifetime is ending. Ideally, we'd only listen to
// the former, but if the RenderProcessHost is destroyed before the
// RenderProcess, then the former is never sent.
if ((type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED ||
type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED)) {
profiled_renderers_.erase(host);
}
}
bool ClientConnectionManager::ShouldProfileNewRenderer(
content::RenderProcessHost* renderer) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// Allow subclasses to not profile renderers.
if (!AllowedToProfileRenderer(renderer))
return false;
Mode mode = GetMode();
if (mode == Mode::kAll || mode == Mode::kAllRenderers) {
return true;
} else if (mode == Mode::kRendererSampling && profiled_renderers_.empty()) {
// Sample renderers with a 1/3 probability.
return (base::RandUint64() % 100000) < 33333;
}
return false;
}
void ClientConnectionManager::StartProfilingRenderer(
content::RenderProcessHost* host,
base::OnceClosure started_profiling_closure) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
profiled_renderers_.insert(host);
mojo::PendingRemote<mojom::ProfilingClient> client;
host->BindReceiver(client.InitWithNewPipeAndPassReceiver());
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&StartProfilingClientOnIOThread, controller_,
std::move(client), host->GetProcess().Pid(),
mojom::ProcessType::RENDERER,
std::move(started_profiling_closure)));
}
} // namespace heap_profiling
|