forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_profile_counter_tree_node.cpp
More file actions
175 lines (153 loc) · 6.77 KB
/
Copy pathruntime_profile_counter_tree_node.cpp
File metadata and controls
175 lines (153 loc) · 6.77 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "runtime/runtime_profile_counter_tree_node.h"
#include <gen_cpp/RuntimeProfile_types.h>
#include <string>
#include "runtime/runtime_profile.h"
namespace doris {
RuntimeProfileCounterTreeNode RuntimeProfileCounterTreeNode::from_map(
const CounterMap& counterMap, const ChildCounterMap& childCounterMap,
const std::string& rootName) {
RuntimeProfileCounterTreeNode rootNode;
rootNode.name = rootName;
if (childCounterMap.empty() || childCounterMap.empty()) {
return rootNode;
}
// ROOT_COUNTER is a special case, it does not have any counter.
if (rootName == RuntimeProfile::ROOT_COUNTER) {
rootNode.counter = nullptr;
} else {
rootNode.counter = counterMap.at(rootName);
}
auto it = childCounterMap.find(rootName);
if (it != childCounterMap.end()) {
for (const auto& childName : it->second) {
rootNode.children.emplace_back(from_map(counterMap, childCounterMap, childName));
}
}
return rootNode;
}
// Prune the tree by:
// 1. Remove all leaf nodes whose level is greater than the given level.
// 2. Remove all nodes whose children are all pruned.
RuntimeProfileCounterTreeNode RuntimeProfileCounterTreeNode::prune_the_tree(
RuntimeProfileCounterTreeNode node, int64_t level) {
// Iterate through the children and prune them recursively
for (auto it = node.children.begin(); it != node.children.end();) {
*it = prune_the_tree(*it, level);
if (it->children.empty() && it->counter->level() > level) {
it = node.children.erase(it);
} else {
++it;
}
}
return node;
}
void RuntimeProfileCounterTreeNode::to_thrift(
std::vector<TCounter>& tcounter,
std::map<std::string, std::set<std::string>>& child_counter_map) const {
if (name != RuntimeProfile::ROOT_COUNTER && counter != nullptr) {
if (auto* highWaterMarkCounter =
dynamic_cast<RuntimeProfile::HighWaterMarkCounter*>(counter)) {
// HighWaterMarkCounter will convert itself to two counters, one is current, the other is peak.
tcounter.push_back(highWaterMarkCounter->to_thrift(name));
tcounter.push_back(highWaterMarkCounter->to_thrift_peak(name + "Peak"));
child_counter_map[highWaterMarkCounter->parent_name()].insert(name + "Peak");
} else if (auto* nonZeroCounter = dynamic_cast<RuntimeProfile::NonZeroCounter*>(counter)) {
if (nonZeroCounter->value() > 0) {
tcounter.push_back(to_thrift());
} else {
// Erase non-zero counter from its parent's child counter map.
child_counter_map[nonZeroCounter->parent_name()].erase(name);
// Adn skipping all child conter of this counter.
return;
}
} else {
tcounter.push_back(to_thrift());
}
}
for (const auto& child : children) {
// insert before child doing to_thrift, so that we can remove it if it is zero.
(child_counter_map)[name].insert(child.name);
child.to_thrift(tcounter, child_counter_map);
}
}
void RuntimeProfileCounterTreeNode::to_proto(
google::protobuf::RepeatedPtrField<PProfileCounter>* proto_counters,
google::protobuf::Map<std::string, PProfileChildCounterSet>* child_counter_map) const {
if (name != RuntimeProfile::ROOT_COUNTER && counter != nullptr) {
if (auto* highWaterMarkCounter =
dynamic_cast<RuntimeProfile::HighWaterMarkCounter*>(counter)) {
// Convert both current and peak values
*proto_counters->Add() = highWaterMarkCounter->to_proto(name);
*proto_counters->Add() = highWaterMarkCounter->to_proto_peak(name + "Peak");
(*(*child_counter_map)[highWaterMarkCounter->parent_name()].mutable_child_counters())
.Add(name + "Peak");
} else if (auto* nonZeroCounter = dynamic_cast<RuntimeProfile::NonZeroCounter*>(counter)) {
if (nonZeroCounter->value() > 0) {
*proto_counters->Add() = to_proto();
} else {
// Skip zero-valued counter and remove from parent's child map
auto it = child_counter_map->find(nonZeroCounter->parent_name());
if (it != child_counter_map->end()) {
auto* set = it->second.mutable_child_counters();
auto remove_it = std::find(set->begin(), set->end(), name);
if (remove_it != set->end()) set->erase(remove_it);
}
return;
}
} else {
*proto_counters->Add() = to_proto();
}
}
for (const auto& child : children) {
(*child_counter_map)[name].add_child_counters(child.name);
child.to_proto(proto_counters, child_counter_map);
}
}
TCounter RuntimeProfileCounterTreeNode::to_thrift() const {
TCounter tcounter;
if (counter != nullptr) {
tcounter = counter->to_thrift(name);
} else {
tcounter.name = name;
}
return tcounter;
}
PProfileCounter RuntimeProfileCounterTreeNode::to_proto() const {
PProfileCounter pcounter;
if (counter != nullptr) {
pcounter = counter->to_proto(name);
} else {
pcounter.set_name(name);
}
return pcounter;
}
void RuntimeProfileCounterTreeNode::pretty_print(std::ostream* s, const std::string& prefix) const {
// Print this node's counter (skip ROOT_COUNTER which has no counter)
if (name != RuntimeProfile::ROOT_COUNTER && counter != nullptr) {
counter->pretty_print(s, prefix, name);
}
// ROOT_COUNTER doesn't print itself, so don't add indentation for its children;
// non-root nodes add " " for their children, matching the old print_child_counters behavior.
const std::string& child_prefix =
(name == RuntimeProfile::ROOT_COUNTER) ? prefix : prefix + " ";
for (const auto& child : children) {
child.pretty_print(s, child_prefix);
}
}
} // namespace doris