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
|
// 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/performance_manager/graph/graph_impl.h"
#include <string_view>
#include "base/containers/contains.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/process/process.h"
#include "base/time/time.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/graph/system_node_impl.h"
#include "components/performance_manager/public/graph/node_data_describer.h"
#include "components/performance_manager/public/graph/node_data_describer_registry.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "components/performance_manager/test_support/mock_graphs.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace performance_manager {
using GraphImplTest = GraphTestHarness;
using ::testing::ElementsAreArray;
TEST_F(GraphImplTest, SafeCasting) {
const Graph* graph_base = graph();
EXPECT_EQ(graph(), GraphImpl::FromGraph(graph_base));
}
TEST_F(GraphImplTest, GetSystemNodeImpl) {
// The SystemNode singleton should be created by default.
EXPECT_NE(nullptr, graph()->GetSystemNodeImpl());
}
TEST_F(GraphImplTest, GetProcessNodeByPid) {
TestNodeWrapper<ProcessNodeImpl> process = CreateNode<ProcessNodeImpl>();
EXPECT_EQ(base::kNullProcessId, process->GetProcessId());
EXPECT_FALSE(process->GetProcess().IsValid());
const base::Process self = base::Process::Current();
EXPECT_EQ(nullptr, graph()->GetProcessNodeByPid(self.Pid()));
process->SetProcess(self.Duplicate(),
/* launch_time=*/base::TimeTicks::Now());
EXPECT_TRUE(process->GetProcess().IsValid());
EXPECT_EQ(self.Pid(), process->GetProcessId());
EXPECT_EQ(process.get(), graph()->GetProcessNodeByPid(self.Pid()));
// Validate that an exited process isn't removed (yet).
process->SetProcessExitStatus(0xCAFE);
EXPECT_FALSE(process->GetProcess().IsValid());
EXPECT_EQ(self.Pid(), process->GetProcessId());
EXPECT_EQ(process.get(), graph()->GetProcessNodeByPid(self.Pid()));
process.reset();
EXPECT_EQ(nullptr, graph()->GetProcessNodeByPid(self.Pid()));
}
TEST_F(GraphImplTest, PIDReuse) {
// This test emulates what happens on Windows under aggressive PID reuse,
// where a process termination notification can be delayed until after the
// PID has been reused for a new process.
static base::Process self = base::Process::Current();
TestNodeWrapper<ProcessNodeImpl> process1 = CreateNode<ProcessNodeImpl>();
TestNodeWrapper<ProcessNodeImpl> process2 = CreateNode<ProcessNodeImpl>();
process1->SetProcess(self.Duplicate(),
/* launch_time=*/base::TimeTicks::Now());
EXPECT_EQ(process1.get(), graph()->GetProcessNodeByPid(self.Pid()));
// First process exits, but hasn't been deleted yet.
process1->SetProcessExitStatus(0xCAFE);
EXPECT_EQ(process1.get(), graph()->GetProcessNodeByPid(self.Pid()));
// The second registration for the same PID should override the first one.
process2->SetProcess(self.Duplicate(),
/* launch_time=*/base::TimeTicks::Now());
EXPECT_EQ(process2.get(), graph()->GetProcessNodeByPid(self.Pid()));
// The destruction of the first process node shouldn't clear the PID
// registration.
process1.reset();
EXPECT_EQ(process2.get(), graph()->GetProcessNodeByPid(self.Pid()));
}
TEST_F(GraphImplTest, GetAllCUsByType) {
MockMultiplePagesInSingleProcessGraph mock_graph(graph());
std::vector<ProcessNodeImpl*> processes =
graph()->GetAllProcessNodeImpls().AsVector();
// Graph contains a browser process and 1 renderer process.
ASSERT_EQ(2u, processes.size());
EXPECT_NE(nullptr, processes[0]);
EXPECT_NE(nullptr, processes[1]);
std::vector<FrameNodeImpl*> frames =
graph()->GetAllFrameNodeImpls().AsVector();
ASSERT_EQ(2u, frames.size());
EXPECT_NE(nullptr, frames[0]);
EXPECT_NE(nullptr, frames[1]);
std::vector<PageNodeImpl*> pages = graph()->GetAllPageNodeImpls().AsVector();
ASSERT_EQ(2u, pages.size());
EXPECT_NE(nullptr, pages[0]);
EXPECT_NE(nullptr, pages[1]);
}
TEST_F(GraphImplTest, GetAllNodes) {
// This mock graphs contains 2 pages with 2 main frames in a single process.
// There is a total of 2 process nodes because of the browser process node.
MockMultiplePagesInSingleProcessGraph mock_graph(graph());
// 1 renderer and 1 browser process.
auto process_nodes = graph()->GetAllProcessNodes().AsVector();
EXPECT_EQ(process_nodes.size(), 2u);
EXPECT_TRUE(base::Contains(process_nodes, mock_graph.process.get()));
// 2 pages.
EXPECT_THAT(graph()->GetAllPageNodes().AsVector(),
::testing::UnorderedElementsAre(mock_graph.page.get(),
mock_graph.other_page.get()));
// 2 frames.
EXPECT_THAT(graph()->GetAllFrameNodes().AsVector(),
::testing::UnorderedElementsAre(mock_graph.frame.get(),
mock_graph.other_frame.get()));
// No workers.
EXPECT_THAT(graph()->GetAllWorkerNodes().AsVector(),
::testing::UnorderedElementsAre());
}
TEST_F(GraphImplTest, GetAllNodeImpls) {
// This mock graphs contains 2 pages with 2 main frames in a single process.
// There is a total of 2 process nodes because of the browser process node.
MockMultiplePagesInSingleProcessGraph mock_graph(graph());
// 1 renderer and 1 browser process.
auto process_nodes = graph()->GetAllProcessNodeImpls().AsVector();
EXPECT_EQ(process_nodes.size(), 2u);
EXPECT_TRUE(base::Contains(process_nodes, mock_graph.process.get()));
// 2 pages.
EXPECT_THAT(graph()->GetAllPageNodeImpls().AsVector(),
::testing::UnorderedElementsAre(mock_graph.page.get(),
mock_graph.other_page.get()));
// 2 frames.
EXPECT_THAT(graph()->GetAllFrameNodeImpls().AsVector(),
::testing::UnorderedElementsAre(mock_graph.frame.get(),
mock_graph.other_frame.get()));
// No workers.
EXPECT_THAT(graph()->GetAllWorkerNodeImpls().AsVector(),
::testing::UnorderedElementsAre());
}
namespace {
class Foo : public GraphOwned {
public:
explicit Foo(int* destructor_count) : destructor_count_(destructor_count) {}
~Foo() override { (*destructor_count_)++; }
// GraphOwned implementation:
void OnPassedToGraph(Graph* graph) override {
EXPECT_EQ(GetOwningGraph(), graph);
passed_to_called_ = true;
}
void OnTakenFromGraph(Graph* graph) override {
EXPECT_EQ(GetOwningGraph(), graph);
taken_from_called_ = true;
}
bool passed_to_called() const { return passed_to_called_; }
bool taken_from_called() const { return taken_from_called_; }
private:
bool passed_to_called_ = false;
bool taken_from_called_ = false;
raw_ptr<int> destructor_count_ = nullptr;
};
} // namespace
TEST_F(GraphImplTest, GraphOwned) {
int destructor_count = 0;
std::unique_ptr<Foo> foo1 = std::make_unique<Foo>(&destructor_count);
std::unique_ptr<Foo> foo2 = std::make_unique<Foo>(&destructor_count);
auto* raw1 = foo1.get();
auto* raw2 = foo2.get();
// Pass both objects to the graph.
std::unique_ptr<GraphImpl> graph = std::make_unique<GraphImpl>();
graph->SetUp();
EXPECT_EQ(0u, graph->GraphOwnedCountForTesting());
EXPECT_FALSE(raw1->passed_to_called());
EXPECT_EQ(raw1->GetOwningGraph(), nullptr);
graph->PassToGraph(std::move(foo1));
EXPECT_TRUE(raw1->passed_to_called());
EXPECT_EQ(raw1->GetOwningGraph(), graph.get());
EXPECT_EQ(1u, graph->GraphOwnedCountForTesting());
EXPECT_FALSE(raw2->passed_to_called());
EXPECT_EQ(raw2->GetOwningGraph(), nullptr);
graph->PassToGraph(std::move(foo2));
EXPECT_TRUE(raw2->passed_to_called());
EXPECT_EQ(raw2->GetOwningGraph(), graph.get());
EXPECT_EQ(2u, graph->GraphOwnedCountForTesting());
// Take one back.
EXPECT_FALSE(raw1->taken_from_called());
foo1 = graph->TakeFromGraphAs<Foo>(raw1);
EXPECT_TRUE(raw1->taken_from_called());
EXPECT_EQ(raw1->GetOwningGraph(), nullptr);
EXPECT_EQ(1u, graph->GraphOwnedCountForTesting());
// Destroy that object and expect its destructor to have been invoked.
EXPECT_EQ(0, destructor_count);
foo1.reset();
EXPECT_EQ(1, destructor_count);
// Now destroy the graph and expect the other object to have been torn down
// too.
graph->TearDown();
graph.reset();
EXPECT_EQ(2, destructor_count);
}
namespace {
class TestNodeDataDescriber : public NodeDataDescriber {
public:
explicit TestNodeDataDescriber(std::string_view name) : name_(name) {}
base::Value::Dict DescribeFrameNodeData(
const FrameNode* node) const override {
base::Value::Dict dict;
dict.Set("name", name_);
dict.Set("type", "FrameNode");
return dict;
}
base::Value::Dict DescribePageNodeData(const PageNode* node) const override {
base::Value::Dict dict;
dict.Set("name", name_);
dict.Set("type", "PageNode");
return dict;
}
base::Value::Dict DescribeProcessNodeData(
const ProcessNode* node) const override {
base::Value::Dict dict;
dict.Set("name", name_);
dict.Set("type", "ProcessNode");
return dict;
}
base::Value::Dict DescribeSystemNodeData(
const SystemNode* node) const override {
base::Value::Dict dict;
dict.Set("name", name_);
dict.Set("type", "SystemNode");
return dict;
}
base::Value::Dict DescribeWorkerNodeData(
const WorkerNode* node) const override {
base::Value::Dict dict;
dict.Set("name", name_);
dict.Set("type", "WorkerNode");
return dict;
}
private:
const std::string name_;
};
void AssertDictValueContainsListKey(const base::Value::Dict& descr,
const char* key,
const std::string s1,
const std::string s2) {
const base::Value::Dict* dict = descr.FindDict(key);
ASSERT_NE(nullptr, dict);
ASSERT_EQ(2u, dict->size());
ASSERT_EQ(*(dict->FindString("name")), s1);
ASSERT_EQ(*(dict->FindString("type")), s2);
}
} // namespace
TEST_F(GraphImplTest, NodeDataDescribers) {
MockSinglePageInSingleProcessGraph mock_graph(graph());
NodeDataDescriberRegistry* registry = graph()->GetNodeDataDescriberRegistry();
// No describers->no description.
base::Value::Dict descr = registry->DescribeNodeData(mock_graph.frame.get());
EXPECT_EQ(0u, descr.size());
// Test that the default impl does nothing.
NodeDataDescriberDefaultImpl default_impl;
registry->RegisterDescriber(&default_impl, "default_impl");
// Test a single non-default describer for each node type.
TestNodeDataDescriber d1("d1");
registry->RegisterDescriber(&d1, "d1");
descr = registry->DescribeNodeData(mock_graph.frame.get());
AssertDictValueContainsListKey(descr, "d1", "d1", "FrameNode");
EXPECT_EQ(1u, descr.size());
descr = registry->DescribeNodeData(mock_graph.page.get());
AssertDictValueContainsListKey(descr, "d1", "d1", "PageNode");
EXPECT_EQ(1u, descr.size());
descr = registry->DescribeNodeData(mock_graph.process.get());
AssertDictValueContainsListKey(descr, "d1", "d1", "ProcessNode");
EXPECT_EQ(1u, descr.size());
descr = registry->DescribeNodeData(graph()->GetSystemNode());
AssertDictValueContainsListKey(descr, "d1", "d1", "SystemNode");
EXPECT_EQ(1u, descr.size());
auto worker = CreateNode<WorkerNodeImpl>(WorkerNode::WorkerType::kDedicated,
mock_graph.process.get());
descr = registry->DescribeNodeData(worker.get());
AssertDictValueContainsListKey(descr, "d1", "d1", "WorkerNode");
EXPECT_EQ(1u, descr.size());
// Unregister the default impl now that it's been verified to say nothing
// about all node types.
registry->UnregisterDescriber(&default_impl);
// Register a second describer and test one node type.
TestNodeDataDescriber d2("d2");
registry->RegisterDescriber(&d2, "d2");
descr = registry->DescribeNodeData(mock_graph.frame.get());
EXPECT_EQ(2u, descr.size());
AssertDictValueContainsListKey(descr, "d1", "d1", "FrameNode");
AssertDictValueContainsListKey(descr, "d2", "d2", "FrameNode");
registry->UnregisterDescriber(&d2);
registry->UnregisterDescriber(&d1);
// No describers after unregistration->no description.
descr = registry->DescribeNodeData(mock_graph.frame.get());
EXPECT_EQ(0u, descr.size());
}
TEST_F(GraphImplTest, OpenersAndEmbeddersClearedOnTeardown) {
auto process = CreateNode<ProcessNodeImpl>();
auto pageA = CreateNode<PageNodeImpl>();
auto frameA1 = CreateFrameNodeAutoId(process.get(), pageA.get());
auto frameA2 =
CreateFrameNodeAutoId(process.get(), pageA.get(), frameA1.get());
auto pageB = CreateNode<PageNodeImpl>();
auto frameB1 = CreateFrameNodeAutoId(process.get(), pageB.get());
auto pageC = CreateNode<PageNodeImpl>();
auto frameC1 = CreateFrameNodeAutoId(process.get(), pageC.get());
// Set up some embedder relationships. These should be gracefully torn down as
// the graph cleans up nodes, otherwise the frame and page node destructors
// will explode.
pageB->SetEmbedderFrameNode(frameA1.get());
pageC->SetOpenerFrameNode(frameA2.get());
}
} // namespace performance_manager
|