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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
// 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 "components/performance_manager/graph/process_node_impl.h"
#include <optional>
#include <utility>
#include <variant>
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/trace_event/named_trigger.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/graph_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/graph/worker_node_impl.h"
#include "components/performance_manager/public/execution_context/execution_context_registry.h"
#include "components/performance_manager/scenarios/browser_performance_scenarios.h"
#include "components/performance_manager/v8_memory/v8_context_tracker.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#include "third_party/perfetto/include/perfetto/tracing/track.h"
namespace performance_manager {
namespace {
// CHECK's that `process_type` is appropriate for a BrowserChildProcessHost and
// returns it. This is called from a ProcessNodeImpl initializer so that the
// type is checked before the constructor body.
content::ProcessType ValidateBrowserChildProcessType(
content::ProcessType process_type) {
CHECK_NE(process_type, content::PROCESS_TYPE_BROWSER);
CHECK_NE(process_type, content::PROCESS_TYPE_RENDERER);
return process_type;
}
} // namespace
ProcessNodeImpl::ProcessNodeImpl(BrowserProcessNodeTag tag)
: ProcessNodeImpl(content::PROCESS_TYPE_BROWSER,
AnyChildProcessHostProxy{},
base::TaskPriority::HIGHEST) {
tracing_track_.emplace(perfetto::ProcessTrack::Current());
}
ProcessNodeImpl::ProcessNodeImpl(RenderProcessHostProxy proxy,
base::TaskPriority priority)
: ProcessNodeImpl(content::PROCESS_TYPE_RENDERER,
AnyChildProcessHostProxy(std::move(proxy)),
priority) {}
ProcessNodeImpl::ProcessNodeImpl(content::ProcessType process_type,
BrowserChildProcessHostProxy proxy)
: ProcessNodeImpl(ValidateBrowserChildProcessType(process_type),
AnyChildProcessHostProxy(std::move(proxy)),
base::TaskPriority::HIGHEST) {}
ProcessNodeImpl::ProcessNodeImpl(content::ProcessType process_type,
AnyChildProcessHostProxy proxy,
base::TaskPriority priority)
: process_type_(process_type),
child_process_host_proxy_(std::move(proxy)),
priority_(priority) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Child process nodes must have a valid proxy.
switch (process_type) {
case content::PROCESS_TYPE_BROWSER:
// Do nothing.
break;
case content::PROCESS_TYPE_RENDERER:
CHECK(std::get<RenderProcessHostProxy>(child_process_host_proxy_)
.is_valid());
break;
default:
CHECK(std::get<BrowserChildProcessHostProxy>(child_process_host_proxy_)
.is_valid());
break;
}
}
ProcessNodeImpl::~ProcessNodeImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Crash if this process node is destroyed while still hosting a worker node.
// TODO(crbug.com/40051698): Turn this into a DCHECK once the issue is
// resolved.
CHECK(worker_nodes_.empty());
}
void ProcessNodeImpl::BindRenderProcessCoordinationUnit(
mojo::PendingReceiver<mojom::ProcessCoordinationUnit> receiver) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// A RenderProcessHost can be reused if the backing process suddenly dies, in
// which case we will receive a new receiver from the newly spawned process.
render_process_receiver_.reset();
render_process_receiver_.Bind(std::move(receiver));
}
void ProcessNodeImpl::BindChildProcessCoordinationUnit(
mojo::PendingReceiver<mojom::ChildProcessCoordinationUnit> receiver) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// A RenderProcessHost can be reused if the backing process suddenly dies, in
// which case we will receive a new receiver from the newly spawned process.
child_process_receiver_.reset();
child_process_receiver_.Bind(std::move(receiver));
}
void ProcessNodeImpl::SetMainThreadTaskLoadIsLow(
bool main_thread_task_load_is_low) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
main_thread_task_load_is_low_.SetAndMaybeNotify(this,
main_thread_task_load_is_low);
}
void ProcessNodeImpl::OnV8ContextCreated(
mojom::V8ContextDescriptionPtr description,
mojom::IframeAttributionDataPtr iframe_attribution_data) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
if (auto* tracker = v8_memory::V8ContextTracker::GetFromGraph(graph())) {
tracker->OnV8ContextCreated(PassKey(), this, *description,
std::move(iframe_attribution_data));
}
}
void ProcessNodeImpl::OnV8ContextDetached(
const blink::V8ContextToken& v8_context_token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
if (auto* tracker = v8_memory::V8ContextTracker::GetFromGraph(graph())) {
tracker->OnV8ContextDetached(PassKey(), this, v8_context_token);
}
}
void ProcessNodeImpl::OnV8ContextDestroyed(
const blink::V8ContextToken& v8_context_token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
if (auto* tracker = v8_memory::V8ContextTracker::GetFromGraph(graph())) {
tracker->OnV8ContextDestroyed(PassKey(), this, v8_context_token);
}
}
void ProcessNodeImpl::OnRemoteIframeAttached(
const blink::LocalFrameToken& parent_frame_token,
const blink::RemoteFrameToken& remote_frame_token,
mojom::IframeAttributionDataPtr iframe_attribution_data) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
// Only dispatch if the frame and its parent still exist when the mojo message
// reaches the browser process. If the frame still exists but now has no
// parent, we don't need to record IframeAttribution data for it since it's
// now unreachable.
//
// An example of this is the custom <webview> element used in Chrome UI
// (extensions/renderer/resources/guest_view/web_view/web_view.js). This
// element has an inner web contents with an opener relationship to the
// webview, but no parent-child relationship. However since it is a custom
// element implemented on top of <iframe>, the renderer has no way to
// distinguish it from a regular iframe. At the moment the contents is
// attached it has a transient parent frame, which is reported through
// OnRemoteIframeAttached, but the parent frame disappears shortly
// afterward.
//
// TODO(crbug.com/40132061): Write an end-to-end browsertest that covers
// this case once all parts of the measure memory API are hooked up.
if (auto* tracker = v8_memory::V8ContextTracker::GetFromGraph(graph())) {
auto* ec_registry =
execution_context::ExecutionContextRegistry::GetFromGraph(graph());
DCHECK(ec_registry);
auto* parent_frame_node =
ec_registry->GetFrameNodeByFrameToken(parent_frame_token);
if (parent_frame_node) {
tracker->OnRemoteIframeAttached(
PassKey(), FrameNodeImpl::FromNode(parent_frame_node),
remote_frame_token, std::move(iframe_attribution_data));
}
}
}
void ProcessNodeImpl::OnRemoteIframeDetached(
const blink::LocalFrameToken& parent_frame_token,
const blink::RemoteFrameToken& remote_frame_token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
if (auto* tracker = v8_memory::V8ContextTracker::GetFromGraph(graph())) {
auto* ec_registry =
execution_context::ExecutionContextRegistry::GetFromGraph(graph());
DCHECK(ec_registry);
auto* parent_frame_node =
ec_registry->GetFrameNodeByFrameToken(parent_frame_token);
if (parent_frame_node) {
tracker->OnRemoteIframeDetached(
PassKey(), FrameNodeImpl::FromNode(parent_frame_node),
remote_frame_token);
}
}
}
void ProcessNodeImpl::InitializeChildProcessCoordination(
uint64_t process_track_id,
InitializeChildProcessCoordinationCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Should not be called for the Browser process, which already has a track.
// Otherwise, it's ok to overwrite `tracing_track_`, since processes can be
// re-initialized for the same ProcessNode (eg. after a crash).
CHECK_NE(process_type_, content::PROCESS_TYPE_BROWSER);
tracing_track_.emplace(perfetto::Track::Global(process_track_id));
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSingleProcess)) {
// The "child process" is actually running in the same process space. The
// global shared memory region is already mapped in, and the per-process
// shared memory region can't be used because regions for different
// "processes" would overwrite each other.
std::move(callback).Run(base::ReadOnlySharedMemoryRegion(),
base::ReadOnlySharedMemoryRegion());
return;
}
std::move(callback).Run(GetGlobalSharedScenarioRegion(),
GetSharedScenarioRegionForProcessNode(this));
}
content::ProcessType ProcessNodeImpl::GetProcessType() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return process_type_;
}
base::ProcessId ProcessNodeImpl::GetProcessId() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return process_id_;
}
const base::Process& ProcessNodeImpl::GetProcess() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return process_.value();
}
resource_attribution::ProcessContext ProcessNodeImpl::GetResourceContext()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return resource_attribution::ProcessContext::FromProcessNode(this);
}
base::TimeTicks ProcessNodeImpl::GetLaunchTime() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return launch_time_;
}
std::optional<int32_t> ProcessNodeImpl::GetExitStatus() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return exit_status_;
}
const std::string& ProcessNodeImpl::GetMetricsName() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return metrics_name_;
}
bool ProcessNodeImpl::GetMainThreadTaskLoadIsLow() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
return main_thread_task_load_is_low_.value();
}
uint64_t ProcessNodeImpl::GetPrivateFootprintKb() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return private_footprint_kb_;
}
uint64_t ProcessNodeImpl::GetResidentSetKb() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return resident_set_kb_;
}
uint64_t ProcessNodeImpl::GetPrivateSwapKb() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return private_swap_kb_;
}
RenderProcessHostId ProcessNodeImpl::GetRenderProcessHostId() const {
return GetRenderProcessHostProxy().render_process_host_id();
}
const RenderProcessHostProxy& ProcessNodeImpl::GetRenderProcessHostProxy()
const {
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
return std::get<RenderProcessHostProxy>(child_process_host_proxy_);
}
const BrowserChildProcessHostProxy&
ProcessNodeImpl::GetBrowserChildProcessHostProxy() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_NE(process_type_, content::PROCESS_TYPE_BROWSER);
DCHECK_NE(process_type_, content::PROCESS_TYPE_RENDERER);
return std::get<BrowserChildProcessHostProxy>(child_process_host_proxy_);
}
base::TaskPriority ProcessNodeImpl::GetPriority() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return priority_.value();
}
ProcessNode::ContentTypes ProcessNodeImpl::GetHostedContentTypes() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
return hosted_content_types_;
}
ProcessNode::NodeSetView<FrameNodeImpl*> ProcessNodeImpl::frame_nodes() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
return NodeSetView<FrameNodeImpl*>(frame_nodes_);
}
ProcessNode::NodeSetView<WorkerNodeImpl*> ProcessNodeImpl::worker_nodes()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
return NodeSetView<WorkerNodeImpl*>(worker_nodes_);
}
std::optional<perfetto::Track> ProcessNodeImpl::tracing_track() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return tracing_track_;
}
void ProcessNodeImpl::SetProcessExitStatus(int32_t exit_status) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// This may occur as the first event seen in the case where the process
// fails to start or suffers a startup crash.
exit_status_ = exit_status;
// Close the process handle to kill the zombie.
process_.SetAndNotify(this, base::Process());
// No more message should be received from this process.
render_process_receiver_.reset();
child_process_receiver_.reset();
}
void ProcessNodeImpl::SetProcessMetricsName(const std::string& metrics_name) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
metrics_name_ = metrics_name;
}
void ProcessNodeImpl::SetProcess(base::Process process,
base::TimeTicks launch_time) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(process.IsValid());
// Either this is the initial process associated with this process node,
// or it's a subsequent process. In the latter case, there must have been
// an exit status associated with the previous process.
DCHECK(!process_.value().IsValid() || exit_status_.has_value());
base::ProcessId pid = process.Pid();
SetProcessImpl(std::move(process), pid, launch_time);
}
void ProcessNodeImpl::AddFrame(FrameNodeImpl* frame_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
const bool inserted = frame_nodes_.insert(frame_node).second;
DCHECK(inserted);
}
void ProcessNodeImpl::RemoveFrame(FrameNodeImpl* frame_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
DCHECK(base::Contains(frame_nodes_, frame_node));
frame_nodes_.erase(frame_node);
}
void ProcessNodeImpl::AddWorker(WorkerNodeImpl* worker_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
const bool inserted = worker_nodes_.insert(worker_node).second;
DCHECK(inserted);
}
void ProcessNodeImpl::RemoveWorker(WorkerNodeImpl* worker_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
DCHECK(base::Contains(worker_nodes_, worker_node));
worker_nodes_.erase(worker_node);
}
void ProcessNodeImpl::set_priority(base::TaskPriority priority) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
priority_.SetAndMaybeNotify(this, priority);
}
void ProcessNodeImpl::add_hosted_content_type(ContentType content_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
hosted_content_types_.Put(content_type);
}
base::WeakPtr<ProcessNodeImpl> ProcessNodeImpl::GetWeakPtr() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return weak_factory_.GetWeakPtr();
}
void ProcessNodeImpl::SetProcessImpl(base::Process process,
base::ProcessId new_pid,
base::TimeTicks launch_time) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(process.IsValid());
graph()->BeforeProcessPidChange(this, new_pid);
// Clear the exit status for the previous process (if any).
exit_status_.reset();
// Also clear the measurement data (if any), as it references the previous
// process.
private_footprint_kb_ = 0;
resident_set_kb_ = 0;
private_swap_kb_ = 0;
process_id_ = new_pid;
launch_time_ = launch_time;
// Set the process variable last, as it will fire the notification.
process_.SetAndNotify(this, std::move(process));
}
ProcessNode::NodeSetView<const FrameNode*> ProcessNodeImpl::GetFrameNodes()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(graph()->NodeEdgesArePublic(this) || frame_nodes_.empty());
return NodeSetView<const FrameNode*>(frame_nodes_);
}
ProcessNode::NodeSetView<const WorkerNode*> ProcessNodeImpl::GetWorkerNodes()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(graph()->NodeEdgesArePublic(this) || worker_nodes_.empty());
return NodeSetView<const WorkerNode*>(worker_nodes_);
}
void ProcessNodeImpl::OnAllFramesInProcessFrozen() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(process_type_, content::PROCESS_TYPE_RENDERER);
for (auto& observer : GetObservers()) {
observer.OnAllFramesInProcessFrozen(this);
}
}
void ProcessNodeImpl::OnInitializingProperties() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
NodeAttachedDataStorage::Create(this);
}
void ProcessNodeImpl::OnUninitializingEdges() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// All child frames should have been removed before the process is removed.
DCHECK(frame_nodes_.empty());
}
void ProcessNodeImpl::CleanUpNodeState() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Make as if we're transitioning to the null PID before we die to clear this
// instance from the PID map.
if (process_id_ != base::kNullProcessId) {
graph()->BeforeProcessPidChange(this, base::kNullProcessId);
}
DestroyNodeInlineDataStorage();
}
} // namespace performance_manager
|