Coverage Report

Created: 2026-04-14 13:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/runtime_profile.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/runtime-profile.cc
19
// and modified by Doris
20
21
#include "runtime/runtime_profile.h"
22
23
#include <gen_cpp/RuntimeProfile_types.h>
24
#include <rapidjson/encodings.h>
25
#include <rapidjson/stringbuffer.h>
26
#include <rapidjson/writer.h>
27
28
#include <algorithm>
29
#include <iomanip>
30
#include <iostream>
31
#include <memory>
32
#include <string>
33
34
#include "common/logging.h"
35
#include "common/object_pool.h"
36
#include "runtime/runtime_profile_counter_tree_node.h"
37
#ifdef BE_TEST
38
#include "common/status.h" // For ErrorCode
39
#endif
40
41
namespace doris {
42
// Thread counters name
43
static const std::string THREAD_VOLUNTARY_CONTEXT_SWITCHES = "VoluntaryContextSwitches";
44
static const std::string THREAD_INVOLUNTARY_CONTEXT_SWITCHES = "InvoluntaryContextSwitches";
45
46
const std::string RuntimeProfile::ROOT_COUNTER;
47
48
3
std::unique_ptr<RuntimeProfile> RuntimeProfile::from_thrift(const TRuntimeProfileTree& node) {
49
3
    if (node.nodes.empty()) {
50
0
        return std::make_unique<RuntimeProfile>("");
51
0
    }
52
3
    TRuntimeProfileNode root_node = node.nodes.front();
53
3
    std::unique_ptr<RuntimeProfile> res = std::make_unique<RuntimeProfile>(root_node.name);
54
3
    res->update(node);
55
3
    return res;
56
3
}
57
58
3
std::unique_ptr<RuntimeProfile> RuntimeProfile::from_proto(const PRuntimeProfileTree& tree) {
59
3
    if (tree.nodes().empty()) {
60
0
        return std::make_unique<RuntimeProfile>("");
61
0
    }
62
63
3
    const PRuntimeProfileNode& root_node = tree.nodes(0);
64
3
    std::unique_ptr<RuntimeProfile> res = std::make_unique<RuntimeProfile>(root_node.name());
65
3
    res->update(tree);
66
3
    return res;
67
3
}
68
69
RuntimeProfile::RuntimeProfile(const std::string& name, bool is_averaged_profile)
70
1.23M
        : _pool(new ObjectPool()),
71
1.23M
          _name(name),
72
1.23M
          _metadata(-1),
73
1.23M
          _timestamp(-1),
74
1.23M
          _is_averaged_profile(is_averaged_profile),
75
1.23M
          _counter_total_time(TUnit::TIME_NS, 0, 3),
76
1.23M
          _local_time_percent(0) {
77
    // TotalTime counter has level3 to disable it from plan profile, because
78
    // it contains its child running time, we use exec time instead.
79
1.23M
    _counter_map["TotalTime"] = &_counter_total_time;
80
1.23M
}
81
82
1.23M
RuntimeProfile::~RuntimeProfile() = default;
83
84
2
bool RuntimeProfile::Counter::operator==(const Counter& other) const {
85
2
    return _value.load(std::memory_order_relaxed) == other._value.load(std::memory_order_relaxed) &&
86
2
           _type == other._type && _level == other._level;
87
2
}
88
89
24
void RuntimeProfile::merge(const RuntimeProfile* other) {
90
24
    DCHECK(other != nullptr);
91
92
    // Merge this level
93
24
    {
94
24
        CounterMap::iterator dst_iter;
95
24
        CounterMap::const_iterator src_iter;
96
24
        std::lock_guard<std::mutex> l(_counter_map_lock);
97
24
        std::lock_guard<std::mutex> m(other->_counter_map_lock);
98
99
68
        for (src_iter = other->_counter_map.begin(); src_iter != other->_counter_map.end();
100
44
             ++src_iter) {
101
44
            dst_iter = _counter_map.find(src_iter->first);
102
103
44
            if (dst_iter == _counter_map.end()) {
104
8
                _counter_map[src_iter->first] = _pool->add(src_iter->second->clone());
105
36
            } else {
106
36
                DCHECK(dst_iter->second->type() == src_iter->second->type());
107
108
36
                if (dst_iter->second->type() == TUnit::DOUBLE_VALUE) {
109
0
                    double new_val =
110
0
                            dst_iter->second->double_value() + src_iter->second->double_value();
111
0
                    dst_iter->second->set(new_val);
112
36
                } else {
113
36
                    dst_iter->second->update(src_iter->second->value());
114
36
                }
115
36
            }
116
44
        }
117
118
24
        ChildCounterMap::const_iterator child_counter_src_itr;
119
120
24
        for (child_counter_src_itr = other->_child_counter_map.begin();
121
42
             child_counter_src_itr != other->_child_counter_map.end(); ++child_counter_src_itr) {
122
18
            _child_counter_map[child_counter_src_itr->first].insert(
123
18
                    child_counter_src_itr->second.begin(), child_counter_src_itr->second.end());
124
18
        }
125
24
    }
126
127
24
    {
128
24
        std::lock_guard<std::mutex> l(_children_lock);
129
24
        std::lock_guard<std::mutex> m(other->_children_lock);
130
131
        // Recursively merge children with matching names
132
40
        for (int i = 0; i < other->_children.size(); ++i) {
133
16
            RuntimeProfile* other_child = other->_children[i].first;
134
16
            ChildMap::iterator j = _child_map.find(other_child->_name);
135
16
            RuntimeProfile* child = nullptr;
136
137
16
            if (j != _child_map.end()) {
138
10
                child = j->second;
139
10
            } else {
140
6
                child = _pool->add(new RuntimeProfile(other_child->_name));
141
6
                child->_local_time_percent = other_child->_local_time_percent;
142
6
                child->_metadata = other_child->_metadata;
143
6
                child->_timestamp = other_child->_timestamp;
144
6
                bool indent_other_child = other->_children[i].second;
145
6
                _child_map[child->_name] = child;
146
6
                _children.push_back(std::make_pair(child, indent_other_child));
147
6
            }
148
149
16
            child->merge(other_child);
150
16
        }
151
24
    }
152
24
}
153
154
9
void RuntimeProfile::update(const TRuntimeProfileTree& thrift_profile) {
155
9
    int idx = 0;
156
9
    update(thrift_profile.nodes, &idx);
157
9
    DCHECK_EQ(idx, thrift_profile.nodes.size());
158
9
}
159
160
9
void RuntimeProfile::update(const PRuntimeProfileTree& proto_profile) {
161
9
    int idx = 0;
162
9
    update(proto_profile.nodes(), &idx);
163
9
    DCHECK_EQ(idx, proto_profile.nodes_size());
164
9
}
165
166
21
void RuntimeProfile::update(const std::vector<TRuntimeProfileNode>& nodes, int* idx) {
167
21
    DCHECK_LT(*idx, nodes.size());
168
21
    const TRuntimeProfileNode& node = nodes[*idx];
169
21
    {
170
21
        std::lock_guard<std::mutex> l(_counter_map_lock);
171
        // update this level
172
21
        std::map<std::string, Counter*>::iterator dst_iter;
173
174
41
        for (int i = 0; i < node.counters.size(); ++i) {
175
20
            const TCounter& tcounter = node.counters[i];
176
20
            CounterMap::iterator j = _counter_map.find(tcounter.name);
177
178
20
            if (j == _counter_map.end()) {
179
14
                _counter_map[tcounter.name] =
180
14
                        _pool->add(new Counter(tcounter.type, tcounter.value));
181
14
            } else {
182
6
                if (j->second->type() != tcounter.type) {
183
0
                    LOG(ERROR) << "Cannot update counters with the same name (" << j->first
184
0
                               << ") but different types.";
185
6
                } else {
186
6
                    j->second->set(tcounter.value);
187
6
                }
188
6
            }
189
20
        }
190
191
21
        ChildCounterMap::const_iterator child_counter_src_itr;
192
193
21
        for (child_counter_src_itr = node.child_counters_map.begin();
194
35
             child_counter_src_itr != node.child_counters_map.end(); ++child_counter_src_itr) {
195
14
            _child_counter_map[child_counter_src_itr->first].insert(
196
14
                    child_counter_src_itr->second.begin(), child_counter_src_itr->second.end());
197
14
        }
198
21
    }
199
200
21
    {
201
21
        std::lock_guard<std::mutex> l(_info_strings_lock);
202
21
        const InfoStrings& info_strings = node.info_strings;
203
21
        for (const std::string& key : node.info_strings_display_order) {
204
            // Look for existing info strings and update in place. If there
205
            // are new strings, add them to the end of the display order.
206
            // TODO: Is nodes.info_strings always a superset of
207
            // _info_strings? If so, can just copy the display order.
208
4
            InfoStrings::const_iterator it = info_strings.find(key);
209
4
            DCHECK(it != info_strings.end());
210
4
            InfoStrings::iterator existing = _info_strings.find(key);
211
212
4
            if (existing == _info_strings.end()) {
213
3
                _info_strings.insert(std::make_pair(key, it->second));
214
3
                _info_strings_display_order.push_back(key);
215
3
            } else {
216
1
                _info_strings[key] = it->second;
217
1
            }
218
4
        }
219
21
    }
220
221
21
    ++*idx;
222
21
    {
223
21
        std::lock_guard<std::mutex> l(_children_lock);
224
225
        // update children with matching names; create new ones if they don't match
226
33
        for (int i = 0; i < node.num_children; ++i) {
227
12
            const TRuntimeProfileNode& tchild = nodes[*idx];
228
12
            ChildMap::iterator j = _child_map.find(tchild.name);
229
12
            RuntimeProfile* child = nullptr;
230
231
12
            if (j != _child_map.end()) {
232
5
                child = j->second;
233
7
            } else {
234
7
                child = _pool->add(new RuntimeProfile(tchild.name));
235
7
                child->_metadata = tchild.metadata;
236
7
                child->_timestamp = tchild.timestamp;
237
7
                _child_map[tchild.name] = child;
238
7
                _children.push_back(std::make_pair(child, tchild.indent));
239
7
            }
240
241
12
            child->update(nodes, idx);
242
12
        }
243
21
    }
244
21
}
245
246
void RuntimeProfile::update(const google::protobuf::RepeatedPtrField<PRuntimeProfileNode>& nodes,
247
21
                            int* idx) {
248
21
    DCHECK_LT(*idx, nodes.size());
249
21
    const PRuntimeProfileNode& node = nodes.Get(*idx);
250
251
21
    {
252
21
        std::lock_guard<std::mutex> l(_counter_map_lock);
253
254
21
        for (const auto& pcounter : node.counters()) {
255
20
            const std::string& name = pcounter.name();
256
20
            auto j = _counter_map.find(name);
257
258
20
            if (j == _counter_map.end()) {
259
14
                _counter_map[name] =
260
14
                        _pool->add(new Counter(unit_to_thrift(pcounter.type()), pcounter.value()));
261
14
            } else {
262
6
                if (unit_to_proto(j->second->type()) != pcounter.type()) {
263
0
                    LOG(ERROR) << "Cannot update counters with the same name (" << name
264
0
                               << ") but different types.";
265
6
                } else {
266
6
                    j->second->set(pcounter.value());
267
6
                }
268
6
            }
269
20
        }
270
271
21
        for (const auto& kv : node.child_counters_map()) {
272
20
            for (const auto& child_name : kv.second.child_counters()) {
273
20
                _child_counter_map[kv.first].insert(child_name);
274
20
            }
275
14
        }
276
21
    }
277
278
21
    {
279
21
        std::lock_guard<std::mutex> l(_info_strings_lock);
280
21
        const auto& info_map = node.info_strings();
281
282
21
        for (const std::string& key : node.info_strings_display_order()) {
283
4
            auto it = info_map.find(key);
284
4
            DCHECK(it != info_map.end());
285
286
4
            auto existing = _info_strings.find(key);
287
4
            if (existing == _info_strings.end()) {
288
3
                _info_strings.insert(std::make_pair(key, it->second));
289
3
                _info_strings_display_order.push_back(key);
290
3
            } else {
291
1
                _info_strings[key] = it->second;
292
1
            }
293
4
        }
294
21
    }
295
296
21
    ++*idx;
297
298
21
    {
299
21
        std::lock_guard<std::mutex> l(_children_lock);
300
33
        for (int i = 0; i < node.num_children(); ++i) {
301
12
            const PRuntimeProfileNode& pchild = nodes.Get(*idx);
302
12
            RuntimeProfile* child = nullptr;
303
304
12
            auto j = _child_map.find(pchild.name());
305
12
            if (j != _child_map.end()) {
306
5
                child = j->second;
307
7
            } else {
308
7
                child = _pool->add(new RuntimeProfile(pchild.name()));
309
7
                child->_metadata = pchild.metadata();
310
7
                child->_timestamp = pchild.timestamp();
311
7
                _child_map[pchild.name()] = child;
312
7
                _children.emplace_back(child, pchild.indent());
313
7
            }
314
315
12
            child->update(nodes, idx);
316
12
        }
317
21
    }
318
21
}
319
320
0
void RuntimeProfile::divide(int n) {
321
0
    DCHECK_GT(n, 0);
322
0
    std::map<std::string, Counter*>::iterator iter;
323
0
    {
324
0
        std::lock_guard<std::mutex> l(_counter_map_lock);
325
326
0
        for (iter = _counter_map.begin(); iter != _counter_map.end(); ++iter) {
327
0
            if (iter->second->type() == TUnit::DOUBLE_VALUE) {
328
0
                iter->second->set(iter->second->double_value() / n);
329
0
            } else {
330
0
                int64_t value = iter->second->_value.load();
331
0
                value = value / n;
332
0
                iter->second->_value.store(value);
333
0
            }
334
0
        }
335
0
    }
336
0
    {
337
0
        std::lock_guard<std::mutex> l(_children_lock);
338
339
0
        for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
340
0
            i->second->divide(n);
341
0
        }
342
0
    }
343
0
}
344
345
0
void RuntimeProfile::clear_children() {
346
0
    std::lock_guard<std::mutex> l(_children_lock);
347
0
    _children.clear();
348
0
}
349
350
0
void RuntimeProfile::compute_time_in_profile() {
351
0
    compute_time_in_profile(total_time_counter()->value());
352
0
}
353
354
0
void RuntimeProfile::compute_time_in_profile(int64_t total) {
355
0
    if (total == 0) {
356
0
        return;
357
0
    }
358
359
    // Add all the total times in all the children
360
0
    int64_t total_child_time = 0;
361
0
    std::lock_guard<std::mutex> l(_children_lock);
362
363
0
    for (int i = 0; i < _children.size(); ++i) {
364
0
        total_child_time += _children[i].first->total_time_counter()->value();
365
0
    }
366
367
0
    int64_t local_time = total_time_counter()->value() - total_child_time;
368
    // Counters have some margin, set to 0 if it was negative.
369
0
    local_time = std::max<int64_t>(0L, local_time);
370
0
    _local_time_percent = static_cast<double>(local_time) / static_cast<double>(total);
371
0
    _local_time_percent = std::min(1.0, _local_time_percent) * 100;
372
373
    // Recurse on children
374
0
    for (int i = 0; i < _children.size(); ++i) {
375
0
        _children[i].first->compute_time_in_profile(total);
376
0
    }
377
0
}
378
379
131
RuntimeProfile* RuntimeProfile::create_child(const std::string& name, bool indent, bool prepend) {
380
131
    std::lock_guard<std::mutex> l(_children_lock);
381
131
    DCHECK(_child_map.find(name) == _child_map.end()) << ", name: " << name;
382
131
    RuntimeProfile* child = _pool->add(new RuntimeProfile(name));
383
131
    if (this->is_set_metadata()) {
384
0
        child->set_metadata(this->metadata());
385
0
    }
386
387
131
    if (_children.empty()) {
388
86
        add_child_unlock(child, indent, nullptr);
389
86
    } else {
390
45
        auto* pos = prepend ? _children.begin()->first : nullptr;
391
45
        add_child_unlock(child, indent, pos);
392
45
    }
393
131
    return child;
394
131
}
395
396
290k
void RuntimeProfile::add_child_unlock(RuntimeProfile* child, bool indent, RuntimeProfile* loc) {
397
290k
    DCHECK(child != nullptr);
398
290k
    _child_map[child->_name] = child;
399
400
290k
    if (loc == nullptr) {
401
290k
        _children.push_back(std::make_pair(child, indent));
402
290k
    } else {
403
37
        for (ChildVector::iterator it = _children.begin(); it != _children.end(); ++it) {
404
37
            if (it->first == loc) {
405
37
                _children.insert(it, std::make_pair(child, indent));
406
37
                return;
407
37
            }
408
37
        }
409
37
        DCHECK(false) << "Invalid loc";
410
0
    }
411
290k
}
412
413
290k
void RuntimeProfile::add_child(RuntimeProfile* child, bool indent, RuntimeProfile* loc) {
414
290k
    std::lock_guard<std::mutex> l(_children_lock);
415
290k
    add_child_unlock(child, indent, loc);
416
290k
}
417
418
784
RuntimeProfile* RuntimeProfile::get_child(std::string name) {
419
784
    std::lock_guard<std::mutex> l(_children_lock);
420
784
    auto it = _child_map.find(name);
421
422
784
    if (it == _child_map.end()) {
423
2
        return nullptr;
424
2
    }
425
426
782
    return it->second;
427
784
}
428
429
4
void RuntimeProfile::get_children(std::vector<RuntimeProfile*>* children) const {
430
4
    children->clear();
431
4
    std::lock_guard<std::mutex> l(_children_lock);
432
433
16
    for (ChildMap::const_iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
434
12
        children->push_back(i->second);
435
12
    }
436
4
}
437
438
0
void RuntimeProfile::get_all_children(std::vector<RuntimeProfile*>* children) {
439
0
    std::lock_guard<std::mutex> l(_children_lock);
440
441
0
    for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
442
0
        children->push_back(i->second);
443
0
        i->second->get_all_children(children);
444
0
    }
445
0
}
446
447
578k
void RuntimeProfile::add_info_string(const std::string& key, const std::string& value) {
448
578k
    std::lock_guard<std::mutex> l(_info_strings_lock);
449
578k
    InfoStrings::iterator it = _info_strings.find(key);
450
451
578k
    if (it == _info_strings.end()) {
452
578k
        _info_strings.insert(std::make_pair(key, value));
453
578k
        _info_strings_display_order.push_back(key);
454
578k
    } else {
455
154
        it->second = value;
456
154
    }
457
578k
}
458
459
33
const std::string* RuntimeProfile::get_info_string(const std::string& key) {
460
33
    std::lock_guard<std::mutex> l(_info_strings_lock);
461
33
    InfoStrings::const_iterator it = _info_strings.find(key);
462
463
33
    if (it == _info_strings.end()) {
464
2
        return nullptr;
465
2
    }
466
467
31
    return &it->second;
468
33
}
469
470
RuntimeProfile::HighWaterMarkCounter* RuntimeProfile::AddHighWaterMarkCounter(
471
        const std::string& name, TUnit::type unit, const std::string& parent_counter_name,
472
121k
        int64_t level) {
473
121k
    DCHECK_EQ(_is_averaged_profile, false);
474
121k
    std::lock_guard<std::mutex> l(_counter_map_lock);
475
121k
    if (_counter_map.find(name) != _counter_map.end()) {
476
83
        return reinterpret_cast<RuntimeProfile::HighWaterMarkCounter*>(_counter_map[name]);
477
83
    }
478
121k
    DCHECK(parent_counter_name == ROOT_COUNTER ||
479
121k
           _counter_map.find(parent_counter_name) != _counter_map.end());
480
121k
    RuntimeProfile::HighWaterMarkCounter* counter =
481
121k
            _pool->add(new RuntimeProfile::HighWaterMarkCounter(unit, level, parent_counter_name));
482
121k
    _counter_map[name] = counter;
483
121k
    _child_counter_map[parent_counter_name].insert(name);
484
121k
    return counter;
485
121k
}
486
487
RuntimeProfile::Counter* RuntimeProfile::add_counter(const std::string& name, TUnit::type type,
488
                                                     const std::string& parent_counter_name,
489
3.80M
                                                     int64_t level) {
490
3.80M
    std::lock_guard<std::mutex> l(_counter_map_lock);
491
492
3.80M
    if (_counter_map.find(name) != _counter_map.end()) {
493
2.52k
        return _counter_map[name];
494
2.52k
    }
495
496
    // Parent counter must already exist.
497
3.80M
    DCHECK(parent_counter_name == ROOT_COUNTER ||
498
3.79M
           _counter_map.find(parent_counter_name) != _counter_map.end());
499
500
3.79M
    Counter* counter = _pool->add(new Counter(type, 0, level));
501
3.79M
    _counter_map[name] = counter;
502
3.79M
    _child_counter_map[parent_counter_name].insert(name);
503
3.79M
    return counter;
504
3.80M
}
505
506
RuntimeProfile::NonZeroCounter* RuntimeProfile::add_nonzero_counter(
507
        const std::string& name, TUnit::type type, const std::string& parent_counter_name,
508
12
        int64_t level) {
509
12
    std::lock_guard<std::mutex> l(_counter_map_lock);
510
12
    if (_counter_map.find(name) != _counter_map.end()) {
511
0
        DCHECK(dynamic_cast<NonZeroCounter*>(_counter_map[name]));
512
0
        return static_cast<NonZeroCounter*>(_counter_map[name]);
513
0
    }
514
515
12
    DCHECK(parent_counter_name == ROOT_COUNTER ||
516
12
           _counter_map.find(parent_counter_name) != _counter_map.end());
517
12
    NonZeroCounter* counter = _pool->add(new NonZeroCounter(type, level, parent_counter_name));
518
12
    _counter_map[name] = counter;
519
12
    _child_counter_map[parent_counter_name].insert(name);
520
12
    return counter;
521
12
}
522
523
RuntimeProfile::DerivedCounter* RuntimeProfile::add_derived_counter(
524
        const std::string& name, TUnit::type type, const DerivedCounterFunction& counter_fn,
525
8
        const std::string& parent_counter_name) {
526
8
    std::lock_guard<std::mutex> l(_counter_map_lock);
527
528
8
    if (_counter_map.find(name) != _counter_map.end()) {
529
0
        return nullptr;
530
0
    }
531
532
8
    DerivedCounter* counter = _pool->add(new DerivedCounter(type, counter_fn));
533
8
    _counter_map[name] = counter;
534
8
    _child_counter_map[parent_counter_name].insert(name);
535
8
    return counter;
536
8
}
537
538
void RuntimeProfile::add_description(const std::string& name, const std::string& description,
539
72.0k
                                     std::string parent_counter_name) {
540
72.0k
    std::lock_guard<std::mutex> l(_counter_map_lock);
541
542
72.0k
    if (_counter_map.find(name) != _counter_map.end()) {
543
0
        Counter* counter = _counter_map[name];
544
0
        if (dynamic_cast<DescriptionEntry*>(counter) != nullptr) {
545
            // Do replace instead of update to avoid data race.
546
0
            _counter_map.erase(name);
547
0
        } else {
548
0
            DCHECK(false) << "Counter type mismatch, name: " << name
549
0
                          << ", type: " << counter->type() << ", description: " << description;
550
0
        }
551
0
    }
552
553
    // Parent counter must already exist.
554
72.0k
    DCHECK(parent_counter_name == ROOT_COUNTER ||
555
72.0k
           _counter_map.find(parent_counter_name) != _counter_map.end());
556
72.0k
    DescriptionEntry* counter = _pool->add(new DescriptionEntry(name, description));
557
72.0k
    _counter_map[name] = counter;
558
72.0k
    _child_counter_map[parent_counter_name].insert(name);
559
72.0k
}
560
561
RuntimeProfile::ConditionCounter* RuntimeProfile::add_conditition_counter(
562
        const std::string& name, TUnit::type type, const ConditionCounterFunction& counter_fn,
563
0
        const std::string& parent_counter_name, int64_t level) {
564
0
    std::lock_guard<std::mutex> l(_counter_map_lock);
565
566
0
    if (_counter_map.find(name) != _counter_map.end()) {
567
0
        RuntimeProfile::ConditionCounter* contition_counter =
568
0
                dynamic_cast<ConditionCounter*>(_counter_map[name]);
569
0
        if (contition_counter == nullptr) {
570
0
            throw doris::Exception(doris::ErrorCode::INTERNAL_ERROR,
571
0
                                   "Failed to add a conditition counter that is duplicate and of a "
572
0
                                   "different type for {}.",
573
0
                                   name);
574
0
        }
575
0
        return contition_counter;
576
0
    }
577
578
0
    ConditionCounter* counter = _pool->add(new ConditionCounter(type, counter_fn, level));
579
0
    _counter_map[name] = counter;
580
0
    _child_counter_map[parent_counter_name].insert(name);
581
0
    return counter;
582
0
}
583
584
6.29k
RuntimeProfile::Counter* RuntimeProfile::get_counter(const std::string& name) {
585
6.29k
    std::lock_guard<std::mutex> l(_counter_map_lock);
586
587
6.29k
    if (_counter_map.find(name) != _counter_map.end()) {
588
5.72k
        return _counter_map[name];
589
5.72k
    }
590
591
569
    return nullptr;
592
6.29k
}
593
594
0
void RuntimeProfile::get_counters(const std::string& name, std::vector<Counter*>* counters) {
595
0
    Counter* c = get_counter(name);
596
597
0
    if (c != nullptr) {
598
0
        counters->push_back(c);
599
0
    }
600
601
0
    std::lock_guard<std::mutex> l(_children_lock);
602
603
0
    for (int i = 0; i < _children.size(); ++i) {
604
0
        _children[i].first->get_counters(name, counters);
605
0
    }
606
0
}
607
608
// Print the profile:
609
//  1. Profile Name
610
//  2. Info Strings
611
//  3. Counters
612
//  4. Children
613
void RuntimeProfile::pretty_print(std::ostream* s, const std::string& prefix,
614
133
                                  int64_t profile_level) const {
615
133
    std::ostream& stream = *s;
616
617
    // create copy of _counter_map and _child_counter_map so we don't need to hold lock
618
    // while we call value() on the counters
619
133
    CounterMap counter_map;
620
133
    ChildCounterMap child_counter_map;
621
133
    {
622
133
        std::lock_guard<std::mutex> l(_counter_map_lock);
623
133
        counter_map = _counter_map;
624
133
        child_counter_map = _child_counter_map;
625
133
    }
626
627
133
    std::map<std::string, Counter*>::const_iterator total_time = counter_map.find("TotalTime");
628
133
    DCHECK(total_time != counter_map.end());
629
630
133
    stream.flags(std::ios::fixed);
631
133
    stream << prefix << _name << ":";
632
633
133
    if (total_time->second->value() != 0) {
634
0
        stream << "(Active: "
635
0
               << PrettyPrinter::print(total_time->second->value(), total_time->second->type())
636
0
               << ", non-child: " << std::setprecision(2) << _local_time_percent << "%)";
637
0
    }
638
639
133
    stream << std::endl;
640
641
133
    {
642
133
        std::lock_guard<std::mutex> l(_info_strings_lock);
643
133
        for (const std::string& key : _info_strings_display_order) {
644
1
            stream << prefix << "   - " << key << ": " << _info_strings.find(key)->second
645
1
                   << std::endl;
646
1
        }
647
133
    }
648
649
    // Build counter tree and prune by profile_level before printing
650
133
    RuntimeProfileCounterTreeNode counter_tree =
651
133
            RuntimeProfileCounterTreeNode::from_map(counter_map, child_counter_map, ROOT_COUNTER);
652
133
    counter_tree = RuntimeProfileCounterTreeNode::prune_the_tree(counter_tree, profile_level);
653
133
    counter_tree.pretty_print(s, prefix);
654
655
    // create copy of _children so we don't need to hold lock while we call
656
    // pretty_print() on the children
657
133
    ChildVector children;
658
133
    {
659
133
        std::lock_guard<std::mutex> l(_children_lock);
660
133
        children = _children;
661
133
    }
662
663
219
    for (int i = 0; i < children.size(); ++i) {
664
86
        RuntimeProfile* profile = children[i].first;
665
86
        bool indent = children[i].second;
666
86
        profile->pretty_print(s, prefix + (indent ? "  " : ""));
667
86
    }
668
133
}
669
670
249
void RuntimeProfile::to_thrift(TRuntimeProfileTree* tree, int64_t profile_level) {
671
249
    tree->nodes.clear();
672
249
    to_thrift(&tree->nodes, profile_level);
673
249
}
674
675
257
void RuntimeProfile::to_thrift(std::vector<TRuntimeProfileNode>* nodes, int64_t profile_level) {
676
257
    size_t index = nodes->size();
677
257
    nodes->push_back(TRuntimeProfileNode());
678
257
    TRuntimeProfileNode& node = (*nodes)[index];
679
257
    node.name = _name;
680
257
    node.metadata = _metadata;
681
257
    node.timestamp = _timestamp;
682
257
    node.indent = true;
683
684
257
    {
685
257
        std::lock_guard<std::mutex> l(_counter_map_lock);
686
257
        RuntimeProfileCounterTreeNode conter_tree = RuntimeProfileCounterTreeNode::from_map(
687
257
                _counter_map, _child_counter_map, ROOT_COUNTER);
688
257
        conter_tree = RuntimeProfileCounterTreeNode::prune_the_tree(conter_tree, profile_level);
689
257
        conter_tree.to_thrift(node.counters, node.child_counters_map);
690
257
    }
691
692
257
    {
693
257
        std::lock_guard<std::mutex> l(_info_strings_lock);
694
257
        node.info_strings = _info_strings;
695
257
        node.info_strings_display_order = _info_strings_display_order;
696
257
    }
697
698
257
    ChildVector children;
699
257
    {
700
        // _children may be modified during to_thrift(),
701
        // so we have to lock and copy _children to avoid race condition
702
257
        std::lock_guard<std::mutex> l(_children_lock);
703
257
        children = _children;
704
257
    }
705
257
    node.num_children = cast_set<int32_t>(children.size());
706
257
    nodes->reserve(nodes->size() + children.size());
707
708
263
    for (int i = 0; i < children.size(); ++i) {
709
6
        size_t child_idx = nodes->size();
710
6
        children[i].first->to_thrift(nodes, profile_level);
711
        // fix up indentation flag
712
6
        (*nodes)[child_idx].indent = children[i].second;
713
6
    }
714
257
}
715
716
5
void RuntimeProfile::to_proto(PRuntimeProfileTree* tree, int64_t profile_level) {
717
5
    tree->clear_nodes();
718
5
    to_proto(tree->mutable_nodes(), profile_level);
719
5
}
720
721
void RuntimeProfile::to_proto(google::protobuf::RepeatedPtrField<PRuntimeProfileNode>* nodes,
722
11
                              int64_t profile_level) {
723
11
    PRuntimeProfileNode* node = nodes->Add(); // allocate new node
724
11
    node->set_name(_name);
725
11
    node->set_metadata(_metadata);
726
11
    node->set_timestamp(_timestamp);
727
11
    node->set_indent(true);
728
729
11
    {
730
11
        std::lock_guard<std::mutex> l(_counter_map_lock);
731
11
        RuntimeProfileCounterTreeNode counter_tree = RuntimeProfileCounterTreeNode::from_map(
732
11
                _counter_map, _child_counter_map, ROOT_COUNTER);
733
11
        counter_tree = RuntimeProfileCounterTreeNode::prune_the_tree(counter_tree, profile_level);
734
11
        counter_tree.to_proto(node->mutable_counters(), node->mutable_child_counters_map());
735
11
    }
736
737
11
    {
738
11
        std::lock_guard<std::mutex> l(_info_strings_lock);
739
11
        auto* info_map = node->mutable_info_strings();
740
11
        for (const auto& kv : _info_strings) {
741
3
            (*info_map)[kv.first] = kv.second;
742
3
        }
743
11
        for (const auto& key : _info_strings_display_order) {
744
3
            node->add_info_strings_display_order(key);
745
3
        }
746
11
    }
747
748
11
    ChildVector children;
749
11
    {
750
11
        std::lock_guard<std::mutex> l(_children_lock);
751
11
        children = _children;
752
11
    }
753
754
11
    node->set_num_children(cast_set<int32_t>(children.size()));
755
756
11
    for (const auto& child : children) {
757
6
        int child_index = cast_set<int>(nodes->size()); // capture index for indent correction
758
6
        child.first->to_proto(nodes, profile_level);
759
6
        (*nodes)[child_index].set_indent(child.second);
760
6
    }
761
11
}
762
763
int64_t RuntimeProfile::units_per_second(const RuntimeProfile::Counter* total_counter,
764
3
                                         const RuntimeProfile::Counter* timer) {
765
3
    DCHECK(total_counter->type() == TUnit::BYTES || total_counter->type() == TUnit::UNIT);
766
3
    DCHECK(timer->type() == TUnit::TIME_NS);
767
768
3
    if (timer->value() == 0) {
769
0
        return 0;
770
0
    }
771
772
3
    double secs = static_cast<double>(timer->value()) / 1000.0 / 1000.0 / 1000.0;
773
3
    return int64_t(static_cast<double>(total_counter->value()) / secs);
774
3
}
775
776
0
int64_t RuntimeProfile::counter_sum(const std::vector<Counter*>* counters) {
777
0
    int64_t value = 0;
778
779
0
    for (int i = 0; i < counters->size(); ++i) {
780
0
        value += (*counters)[i]->value();
781
0
    }
782
783
0
    return value;
784
0
}
785
786
void RuntimeProfile::print_child_counters(const std::string& prefix,
787
                                          const std::string& counter_name,
788
                                          const CounterMap& counter_map,
789
                                          const ChildCounterMap& child_counter_map,
790
0
                                          std::ostream* s) {
791
0
    auto itr = child_counter_map.find(counter_name);
792
793
0
    if (itr != child_counter_map.end()) {
794
0
        const std::set<std::string>& child_counters = itr->second;
795
0
        for (const std::string& child_counter : child_counters) {
796
0
            auto iter = counter_map.find(child_counter);
797
            DCHECK(iter != counter_map.end());
798
0
            iter->second->pretty_print(s, prefix, iter->first);
799
0
            RuntimeProfile::print_child_counters(prefix + "  ", child_counter, counter_map,
800
0
                                                 child_counter_map, s);
801
0
        }
802
0
    }
803
0
}
804
805
} // namespace doris