Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef SIMPLE_PERF_CALLCHAIN_H_ |
| 18 | #define SIMPLE_PERF_CALLCHAIN_H_ |
| 19 | |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
| 22 | #include <algorithm> |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 23 | #include <functional> |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 24 | #include <memory> |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 25 | #include <queue> |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 28 | #include <android-base/logging.h> |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 29 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 30 | namespace simpleperf { |
| 31 | |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 32 | template <typename EntryT> |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 33 | struct CallChainNode { |
| 34 | uint64_t period; |
| 35 | uint64_t children_period; |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 36 | std::vector<EntryT*> chain; |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 37 | std::vector<std::unique_ptr<CallChainNode>> children; |
| 38 | }; |
| 39 | |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 40 | template <typename EntryT> |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 41 | struct CallChainRoot { |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 42 | typedef CallChainNode<EntryT> NodeT; |
Yabin Cui | 0c093f3 | 2017-04-19 11:48:44 -0700 | [diff] [blame] | 43 | // If duplicated = true, this call tree is part of another call tree. |
| 44 | // And we don't need to show it in brief callgraph report mode. |
| 45 | bool duplicated; |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 46 | uint64_t children_period; |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 47 | std::vector<std::unique_ptr<NodeT>> children; |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 48 | |
Yabin Cui | 0c093f3 | 2017-04-19 11:48:44 -0700 | [diff] [blame] | 49 | CallChainRoot() : duplicated(false), children_period(0) {} |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 50 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 51 | void AddCallChain(const std::vector<EntryT*>& callchain, uint64_t period, |
| 52 | std::function<bool(const EntryT*, const EntryT*)> is_same_sample) { |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 53 | children_period += period; |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 54 | NodeT* p = FindMatchingNode(children, callchain[0], is_same_sample); |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 55 | if (p == nullptr) { |
| 56 | std::unique_ptr<NodeT> new_node = AllocateNode(callchain, 0, period, 0); |
| 57 | children.push_back(std::move(new_node)); |
| 58 | return; |
| 59 | } |
| 60 | size_t callchain_pos = 0; |
| 61 | while (true) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 62 | size_t match_length = GetMatchingLengthInNode(p, callchain, callchain_pos, is_same_sample); |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 63 | CHECK_GT(match_length, 0u); |
| 64 | callchain_pos += match_length; |
| 65 | bool find_child = true; |
| 66 | if (match_length < p->chain.size()) { |
| 67 | SplitNode(p, match_length); |
| 68 | find_child = false; // No need to find matching node in p->children. |
| 69 | } |
| 70 | if (callchain_pos == callchain.size()) { |
| 71 | p->period += period; |
| 72 | return; |
| 73 | } |
| 74 | p->children_period += period; |
| 75 | if (find_child) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 76 | NodeT* np = FindMatchingNode(p->children, callchain[callchain_pos], is_same_sample); |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 77 | if (np != nullptr) { |
| 78 | p = np; |
| 79 | continue; |
| 80 | } |
| 81 | } |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 82 | std::unique_ptr<NodeT> new_node = AllocateNode(callchain, callchain_pos, period, 0); |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 83 | p->children.push_back(std::move(new_node)); |
| 84 | break; |
| 85 | } |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 88 | void SortByPeriod() { |
| 89 | std::queue<std::vector<std::unique_ptr<NodeT>>*> queue; |
| 90 | queue.push(&children); |
| 91 | while (!queue.empty()) { |
| 92 | std::vector<std::unique_ptr<NodeT>>* v = queue.front(); |
| 93 | queue.pop(); |
| 94 | std::sort(v->begin(), v->end(), CallChainRoot::CompareNodeByPeriod); |
| 95 | for (auto& node : *v) { |
| 96 | if (!node->children.empty()) { |
| 97 | queue.push(&node->children); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private: |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 104 | NodeT* FindMatchingNode(const std::vector<std::unique_ptr<NodeT>>& nodes, const EntryT* sample, |
| 105 | std::function<bool(const EntryT*, const EntryT*)> is_same_sample) { |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 106 | for (auto& node : nodes) { |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 107 | if (is_same_sample(node->chain.front(), sample)) { |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 108 | return node.get(); |
| 109 | } |
| 110 | } |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 114 | size_t GetMatchingLengthInNode(NodeT* node, const std::vector<EntryT*>& chain, size_t chain_start, |
| 115 | std::function<bool(const EntryT*, const EntryT*)> is_same_sample) { |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 116 | size_t i, j; |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 117 | for (i = 0, j = chain_start; i < node->chain.size() && j < chain.size(); ++i, ++j) { |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 118 | if (!is_same_sample(node->chain[i], chain[j])) { |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | } |
| 122 | return i; |
| 123 | } |
| 124 | |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 125 | void SplitNode(NodeT* parent, size_t parent_length) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 126 | std::unique_ptr<NodeT> child = |
| 127 | AllocateNode(parent->chain, parent_length, parent->period, parent->children_period); |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 128 | child->children = std::move(parent->children); |
| 129 | parent->period = 0; |
| 130 | parent->children_period = child->period + child->children_period; |
| 131 | parent->chain.resize(parent_length); |
| 132 | parent->children.clear(); |
| 133 | parent->children.push_back(std::move(child)); |
| 134 | } |
| 135 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 136 | std::unique_ptr<NodeT> AllocateNode(const std::vector<EntryT*>& chain, size_t chain_start, |
| 137 | uint64_t period, uint64_t children_period) { |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 138 | std::unique_ptr<NodeT> node(new NodeT); |
| 139 | for (size_t i = chain_start; i < chain.size(); ++i) { |
| 140 | node->chain.push_back(chain[i]); |
| 141 | } |
| 142 | node->period = period; |
| 143 | node->children_period = children_period; |
| 144 | return node; |
| 145 | } |
| 146 | |
| 147 | static bool CompareNodeByPeriod(const std::unique_ptr<NodeT>& n1, |
| 148 | const std::unique_ptr<NodeT>& n2) { |
| 149 | uint64_t period1 = n1->period + n1->children_period; |
| 150 | uint64_t period2 = n2->period + n2->children_period; |
| 151 | return period1 > period2; |
| 152 | } |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 155 | } // namespace simpleperf |
| 156 | |
Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 157 | #endif // SIMPLE_PERF_CALLCHAIN_H_ |