1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/trace_processor/importers/memory_tracker/raw_process_memory_node.h"
18
19 #include <stddef.h>
20
21 #include <unordered_map>
22
23 #include "perfetto/base/build_config.h"
24 #include "test/gtest_and_gmock.h"
25
26 namespace perfetto {
27 namespace trace_processor {
28
29 namespace {
30
31 const LevelOfDetail kLevelOfDetail = LevelOfDetail::kDetailed;
32
33 } // namespace
34
TEST(RawProcessMemoryNodeTest,MoveConstructor)35 TEST(RawProcessMemoryNodeTest, MoveConstructor) {
36 const auto source = MemoryAllocatorNodeId(42);
37 const auto target = MemoryAllocatorNodeId(4242);
38
39 std::unique_ptr<RawMemoryGraphNode> mad1(
40 new RawMemoryGraphNode("mad1", kLevelOfDetail, source));
41 std::unique_ptr<RawMemoryGraphNode> mad2(
42 new RawMemoryGraphNode("mad2", kLevelOfDetail, target));
43
44 RawProcessMemoryNode::MemoryNodesMap nodesMap;
45 nodesMap.emplace(mad1->absolute_name(), std::move(mad1));
46 nodesMap.emplace(mad2->absolute_name(), std::move(mad2));
47
48 std::unique_ptr<MemoryGraphEdge> edge(
49 new MemoryGraphEdge(source, target, 10, false));
50
51 RawProcessMemoryNode::AllocatorNodeEdgesMap edgesMap;
52 edgesMap.emplace(edge->source, std::move(edge));
53
54 RawProcessMemoryNode pmd1(kLevelOfDetail, std::move(edgesMap),
55 std::move(nodesMap));
56
57 RawProcessMemoryNode pmd2(std::move(pmd1));
58
59 EXPECT_EQ(1u, pmd2.allocator_nodes().count("mad1"));
60 EXPECT_EQ(1u, pmd2.allocator_nodes().count("mad2"));
61 EXPECT_EQ(LevelOfDetail::kDetailed, pmd2.level_of_detail());
62 EXPECT_EQ(1u, pmd2.allocator_nodes_edges().size());
63 }
64
TEST(RawProcessMemoryNodeTest,MoveAssignment)65 TEST(RawProcessMemoryNodeTest, MoveAssignment) {
66 const auto source = MemoryAllocatorNodeId(42);
67 const auto target = MemoryAllocatorNodeId(4242);
68
69 std::unique_ptr<RawMemoryGraphNode> mad1(
70 new RawMemoryGraphNode("mad1", kLevelOfDetail, source));
71 std::unique_ptr<RawMemoryGraphNode> mad2(
72 new RawMemoryGraphNode("mad2", kLevelOfDetail, target));
73
74 RawProcessMemoryNode::MemoryNodesMap nodesMap;
75 nodesMap.emplace(mad1->absolute_name(), std::move(mad1));
76 nodesMap.emplace(mad2->absolute_name(), std::move(mad2));
77
78 std::unique_ptr<MemoryGraphEdge> edge(
79 new MemoryGraphEdge(source, target, 10, false));
80
81 RawProcessMemoryNode::AllocatorNodeEdgesMap edgesMap;
82 edgesMap.emplace(edge->source, std::move(edge));
83
84 RawProcessMemoryNode pmd1(kLevelOfDetail, std::move(edgesMap),
85 std::move(nodesMap));
86
87 RawProcessMemoryNode pmd2(LevelOfDetail::kBackground);
88
89 pmd2 = std::move(pmd1);
90 EXPECT_EQ(1u, pmd2.allocator_nodes().count("mad1"));
91 EXPECT_EQ(1u, pmd2.allocator_nodes().count("mad2"));
92 EXPECT_EQ(0u, pmd2.allocator_nodes().count("mad3"));
93 EXPECT_EQ(LevelOfDetail::kDetailed, pmd2.level_of_detail());
94 EXPECT_EQ(1u, pmd2.allocator_nodes_edges().size());
95 }
96
97 } // namespace trace_processor
98 } // namespace perfetto
99