Coverage Report

Created: 2026-03-21 03:59

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