Coverage Report

Created: 2025-04-24 13:42

/root/doris/be/src/util/runtime_profile.cpp
Line
Count
Source (jump to first uncovered line)
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 "util/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 "util/container_util.hpp"
37
#include "util/runtime_profile_counter_tree_node.h"
38
#ifdef BE_TEST
39
#include "common/status.h" // For ErrorCode
40
#endif
41
42
namespace doris {
43
44
// Thread counters name
45
static const std::string THREAD_TOTAL_TIME = "TotalWallClockTime";
46
static const std::string THREAD_VOLUNTARY_CONTEXT_SWITCHES = "VoluntaryContextSwitches";
47
static const std::string THREAD_INVOLUNTARY_CONTEXT_SWITCHES = "InvoluntaryContextSwitches";
48
49
const std::string RuntimeProfile::ROOT_COUNTER;
50
51
3
std::unique_ptr<RuntimeProfile> RuntimeProfile::from_thrift(const TRuntimeProfileTree& node) {
52
3
    if (node.nodes.empty()) {
53
0
        return std::make_unique<RuntimeProfile>("");
54
0
    }
55
3
    TRuntimeProfileNode root_node = node.nodes.front();
56
3
    std::unique_ptr<RuntimeProfile> res = std::make_unique<RuntimeProfile>(root_node.name);
57
3
    res->update(node);
58
3
    return res;
59
3
}
60
61
RuntimeProfile::RuntimeProfile(const std::string& name, bool is_averaged_profile)
62
        : _pool(new ObjectPool()),
63
          _name(name),
64
          _metadata(-1),
65
          _timestamp(-1),
66
          _is_averaged_profile(is_averaged_profile),
67
          _counter_total_time(TUnit::TIME_NS, 0, 3),
68
756k
          _local_time_percent(0) {
69
    // TotalTime counter has level3 to disable it from plan profile, because
70
    // it contains its child running time, we use exec time instead.
71
756k
    _counter_map["TotalTime"] = &_counter_total_time;
72
756k
}
73
74
756k
RuntimeProfile::~RuntimeProfile() = default;
75
76
2
bool RuntimeProfile::Counter::operator==(const Counter& other) const {
77
2
    return _value.load(std::memory_order_relaxed) == other._value.load(std::memory_order_relaxed) &&
78
2
           _type == other._type && _level == other._level;
79
2
}
80
81
12
void RuntimeProfile::merge(RuntimeProfile* other) {
82
12
    DCHECK(other != nullptr);
83
84
    // Merge this level
85
12
    {
86
12
        CounterMap::iterator dst_iter;
87
12
        CounterMap::const_iterator src_iter;
88
12
        std::lock_guard<std::mutex> l(_counter_map_lock);
89
12
        std::lock_guard<std::mutex> m(other->_counter_map_lock);
90
91
34
        for (src_iter = other->_counter_map.begin(); src_iter != other->_counter_map.end();
92
22
             ++src_iter) {
93
22
            dst_iter = _counter_map.find(src_iter->first);
94
95
22
            if (dst_iter == _counter_map.end()) {
96
4
                _counter_map[src_iter->first] = _pool->add(src_iter->second->clone());
97
18
            } else {
98
18
                DCHECK(dst_iter->second->type() == src_iter->second->type());
99
100
18
                if (dst_iter->second->type() == TUnit::DOUBLE_VALUE) {
101
0
                    double new_val =
102
0
                            dst_iter->second->double_value() + src_iter->second->double_value();
103
0
                    dst_iter->second->set(new_val);
104
18
                } else {
105
18
                    dst_iter->second->update(src_iter->second->value());
106
18
                }
107
18
            }
108
22
        }
109
110
12
        ChildCounterMap::const_iterator child_counter_src_itr;
111
112
12
        for (child_counter_src_itr = other->_child_counter_map.begin();
113
21
             child_counter_src_itr != other->_child_counter_map.end(); ++child_counter_src_itr) {
114
9
            std::set<std::string>* child_counters = find_or_insert(
115
9
                    &_child_counter_map, child_counter_src_itr->first, std::set<std::string>());
116
9
            child_counters->insert(child_counter_src_itr->second.begin(),
117
9
                                   child_counter_src_itr->second.end());
118
9
        }
119
12
    }
120
121
12
    {
122
12
        std::lock_guard<std::mutex> l(_children_lock);
123
12
        std::lock_guard<std::mutex> m(other->_children_lock);
124
125
        // Recursively merge children with matching names
126
20
        for (int i = 0; i < other->_children.size(); ++i) {
127
8
            RuntimeProfile* other_child = other->_children[i].first;
128
8
            ChildMap::iterator j = _child_map.find(other_child->_name);
129
8
            RuntimeProfile* child = nullptr;
130
131
8
            if (j != _child_map.end()) {
132
5
                child = j->second;
133
5
            } else {
134
3
                child = _pool->add(new RuntimeProfile(other_child->_name));
135
3
                child->_local_time_percent = other_child->_local_time_percent;
136
3
                child->_metadata = other_child->_metadata;
137
3
                child->_timestamp = other_child->_timestamp;
138
3
                bool indent_other_child = other->_children[i].second;
139
3
                _child_map[child->_name] = child;
140
3
                _children.push_back(std::make_pair(child, indent_other_child));
141
3
            }
142
143
8
            child->merge(other_child);
144
8
        }
145
12
    }
146
12
}
147
148
9
void RuntimeProfile::update(const TRuntimeProfileTree& thrift_profile) {
149
9
    int idx = 0;
150
9
    update(thrift_profile.nodes, &idx);
151
9
    DCHECK_EQ(idx, thrift_profile.nodes.size());
152
9
}
153
154
21
void RuntimeProfile::update(const std::vector<TRuntimeProfileNode>& nodes, int* idx) {
155
21
    DCHECK_LT(*idx, nodes.size());
156
21
    const TRuntimeProfileNode& node = nodes[*idx];
157
21
    {
158
21
        std::lock_guard<std::mutex> l(_counter_map_lock);
159
        // update this level
160
21
        std::map<std::string, Counter*>::iterator dst_iter;
161
162
41
        for (int i = 0; i < node.counters.size(); ++i) {
163
20
            const TCounter& tcounter = node.counters[i];
164
20
            CounterMap::iterator j = _counter_map.find(tcounter.name);
165
166
20
            if (j == _counter_map.end()) {
167
14
                _counter_map[tcounter.name] =
168
14
                        _pool->add(new Counter(tcounter.type, tcounter.value));
169
14
            } else {
170
6
                if (j->second->type() != tcounter.type) {
171
0
                    LOG(ERROR) << "Cannot update counters with the same name (" << j->first
172
0
                               << ") but different types.";
173
6
                } else {
174
6
                    j->second->set(tcounter.value);
175
6
                }
176
6
            }
177
20
        }
178
179
21
        ChildCounterMap::const_iterator child_counter_src_itr;
180
181
21
        for (child_counter_src_itr = node.child_counters_map.begin();
182
35
             child_counter_src_itr != node.child_counters_map.end(); ++child_counter_src_itr) {
183
14
            std::set<std::string>* child_counters = find_or_insert(
184
14
                    &_child_counter_map, child_counter_src_itr->first, std::set<std::string>());
185
14
            child_counters->insert(child_counter_src_itr->second.begin(),
186
14
                                   child_counter_src_itr->second.end());
187
14
        }
188
21
    }
189
190
21
    {
191
21
        std::lock_guard<std::mutex> l(_info_strings_lock);
192
21
        const InfoStrings& info_strings = node.info_strings;
193
21
        for (const std::string& key : node.info_strings_display_order) {
194
            // Look for existing info strings and update in place. If there
195
            // are new strings, add them to the end of the display order.
196
            // TODO: Is nodes.info_strings always a superset of
197
            // _info_strings? If so, can just copy the display order.
198
4
            InfoStrings::const_iterator it = info_strings.find(key);
199
4
            DCHECK(it != info_strings.end());
200
4
            InfoStrings::iterator existing = _info_strings.find(key);
201
202
4
            if (existing == _info_strings.end()) {
203
3
                _info_strings.insert(std::make_pair(key, it->second));
204
3
                _info_strings_display_order.push_back(key);
205
3
            } else {
206
1
                _info_strings[key] = it->second;
207
1
            }
208
4
        }
209
21
    }
210
211
21
    ++*idx;
212
21
    {
213
21
        std::lock_guard<std::mutex> l(_children_lock);
214
215
        // update children with matching names; create new ones if they don't match
216
33
        for (int i = 0; i < node.num_children; ++i) {
217
12
            const TRuntimeProfileNode& tchild = nodes[*idx];
218
12
            ChildMap::iterator j = _child_map.find(tchild.name);
219
12
            RuntimeProfile* child = nullptr;
220
221
12
            if (j != _child_map.end()) {
222
5
                child = j->second;
223
7
            } else {
224
7
                child = _pool->add(new RuntimeProfile(tchild.name));
225
7
                child->_metadata = tchild.metadata;
226
7
                child->_timestamp = tchild.timestamp;
227
7
                _child_map[tchild.name] = child;
228
7
                _children.push_back(std::make_pair(child, tchild.indent));
229
7
            }
230
231
12
            child->update(nodes, idx);
232
12
        }
233
21
    }
234
21
}
235
236
0
void RuntimeProfile::divide(int n) {
237
0
    DCHECK_GT(n, 0);
238
0
    std::map<std::string, Counter*>::iterator iter;
239
0
    {
240
0
        std::lock_guard<std::mutex> l(_counter_map_lock);
241
242
0
        for (iter = _counter_map.begin(); iter != _counter_map.end(); ++iter) {
243
0
            if (iter->second->type() == TUnit::DOUBLE_VALUE) {
244
0
                iter->second->set(iter->second->double_value() / n);
245
0
            } else {
246
0
                int64_t value = iter->second->_value.load();
247
0
                value = value / n;
248
0
                iter->second->_value.store(value);
249
0
            }
250
0
        }
251
0
    }
252
0
    {
253
0
        std::lock_guard<std::mutex> l(_children_lock);
254
255
0
        for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
256
0
            i->second->divide(n);
257
0
        }
258
0
    }
259
0
}
260
261
0
void RuntimeProfile::clear_children() {
262
0
    std::lock_guard<std::mutex> l(_children_lock);
263
0
    _children.clear();
264
0
}
265
266
0
void RuntimeProfile::compute_time_in_profile() {
267
0
    compute_time_in_profile(total_time_counter()->value());
268
0
}
269
270
0
void RuntimeProfile::compute_time_in_profile(int64_t total) {
271
0
    if (total == 0) {
272
0
        return;
273
0
    }
274
275
    // Add all the total times in all the children
276
0
    int64_t total_child_time = 0;
277
0
    std::lock_guard<std::mutex> l(_children_lock);
278
279
0
    for (int i = 0; i < _children.size(); ++i) {
280
0
        total_child_time += _children[i].first->total_time_counter()->value();
281
0
    }
282
283
0
    int64_t local_time = total_time_counter()->value() - total_child_time;
284
    // Counters have some margin, set to 0 if it was negative.
285
0
    local_time = std::max<int64_t>(0L, local_time);
286
0
    _local_time_percent = static_cast<double>(local_time) / total;
287
0
    _local_time_percent = std::min(1.0, _local_time_percent) * 100;
288
289
    // Recurse on children
290
0
    for (int i = 0; i < _children.size(); ++i) {
291
0
        _children[i].first->compute_time_in_profile(total);
292
0
    }
293
0
}
294
295
115
RuntimeProfile* RuntimeProfile::create_child(const std::string& name, bool indent, bool prepend) {
296
115
    std::lock_guard<std::mutex> l(_children_lock);
297
115
    DCHECK(_child_map.find(name) == _child_map.end()) << ", name: " << name;
298
115
    RuntimeProfile* child = _pool->add(new RuntimeProfile(name));
299
115
    if (this->is_set_metadata()) {
300
0
        child->set_metadata(this->metadata());
301
0
    }
302
303
115
    if (_children.empty()) {
304
77
        add_child_unlock(child, indent, nullptr);
305
77
    } else {
306
38
        auto* pos = prepend ? _children.begin()->first : nullptr;
307
38
        add_child_unlock(child, indent, pos);
308
38
    }
309
115
    return child;
310
115
}
311
312
104k
void RuntimeProfile::add_child_unlock(RuntimeProfile* child, bool indent, RuntimeProfile* loc) {
313
104k
    DCHECK(child != nullptr);
314
104k
    _child_map[child->_name] = child;
315
316
104k
    if (loc == nullptr) {
317
104k
        _children.push_back(std::make_pair(child, indent));
318
104k
    } else {
319
33
        for (ChildVector::iterator it = _children.begin(); it != _children.end(); ++it) {
320
33
            if (it->first == loc) {
321
33
                _children.insert(it, std::make_pair(child, indent));
322
33
                return;
323
33
            }
324
33
        }
325
0
        DCHECK(false) << "Invalid loc";
326
0
    }
327
104k
}
328
329
104k
void RuntimeProfile::add_child(RuntimeProfile* child, bool indent, RuntimeProfile* loc) {
330
104k
    std::lock_guard<std::mutex> l(_children_lock);
331
104k
    add_child_unlock(child, indent, loc);
332
104k
}
333
334
2
void RuntimeProfile::get_children(std::vector<RuntimeProfile*>* children) {
335
2
    children->clear();
336
2
    std::lock_guard<std::mutex> l(_children_lock);
337
338
8
    for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
339
6
        children->push_back(i->second);
340
6
    }
341
2
}
342
343
0
void RuntimeProfile::get_all_children(std::vector<RuntimeProfile*>* children) {
344
0
    std::lock_guard<std::mutex> l(_children_lock);
345
346
0
    for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
347
0
        children->push_back(i->second);
348
0
        i->second->get_all_children(children);
349
0
    }
350
0
}
351
352
312k
void RuntimeProfile::add_info_string(const std::string& key, const std::string& value) {
353
312k
    std::lock_guard<std::mutex> l(_info_strings_lock);
354
312k
    InfoStrings::iterator it = _info_strings.find(key);
355
356
312k
    if (it == _info_strings.end()) {
357
312k
        _info_strings.insert(std::make_pair(key, value));
358
312k
        _info_strings_display_order.push_back(key);
359
312k
    } else {
360
165
        it->second = value;
361
165
    }
362
312k
}
363
364
16
const std::string* RuntimeProfile::get_info_string(const std::string& key) {
365
16
    std::lock_guard<std::mutex> l(_info_strings_lock);
366
16
    InfoStrings::const_iterator it = _info_strings.find(key);
367
368
16
    if (it == _info_strings.end()) {
369
1
        return nullptr;
370
1
    }
371
372
15
    return &it->second;
373
16
}
374
375
RuntimeProfile::HighWaterMarkCounter* RuntimeProfile::AddHighWaterMarkCounter(
376
        const std::string& name, TUnit::type unit, const std::string& parent_counter_name,
377
130k
        int64_t level) {
378
130k
    DCHECK_EQ(_is_averaged_profile, false);
379
130k
    std::lock_guard<std::mutex> l(_counter_map_lock);
380
130k
    if (_counter_map.find(name) != _counter_map.end()) {
381
51
        return reinterpret_cast<RuntimeProfile::HighWaterMarkCounter*>(_counter_map[name]);
382
51
    }
383
130k
    DCHECK(parent_counter_name == ROOT_COUNTER ||
384
130k
           _counter_map.find(parent_counter_name) != _counter_map.end());
385
130k
    RuntimeProfile::HighWaterMarkCounter* counter =
386
130k
            _pool->add(new RuntimeProfile::HighWaterMarkCounter(unit, level, parent_counter_name));
387
130k
    _counter_map[name] = counter;
388
130k
    std::set<std::string>* child_counters =
389
130k
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
390
130k
    child_counters->insert(name);
391
130k
    return counter;
392
130k
}
393
394
RuntimeProfile::Counter* RuntimeProfile::add_counter(const std::string& name, TUnit::type type,
395
                                                     const std::string& parent_counter_name,
396
3.09M
                                                     int64_t level) {
397
3.09M
    std::lock_guard<std::mutex> l(_counter_map_lock);
398
399
3.09M
    if (_counter_map.find(name) != _counter_map.end()) {
400
1.56k
        return _counter_map[name];
401
1.56k
    }
402
403
    // Parent counter must already exist.
404
3.09M
    DCHECK(parent_counter_name == ROOT_COUNTER ||
405
3.09M
           _counter_map.find(parent_counter_name) != _counter_map.end());
406
407
3.09M
    Counter* counter = _pool->add(new Counter(type, 0, level));
408
3.09M
    _counter_map[name] = counter;
409
3.09M
    std::set<std::string>* child_counters =
410
3.09M
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
411
3.09M
    child_counters->insert(name);
412
3.09M
    return counter;
413
3.09M
}
414
415
RuntimeProfile::NonZeroCounter* RuntimeProfile::add_nonzero_counter(
416
        const std::string& name, TUnit::type type, const std::string& parent_counter_name,
417
32
        int64_t level) {
418
32
    std::lock_guard<std::mutex> l(_counter_map_lock);
419
32
    if (_counter_map.find(name) != _counter_map.end()) {
420
0
        DCHECK(dynamic_cast<NonZeroCounter*>(_counter_map[name]));
421
0
        return static_cast<NonZeroCounter*>(_counter_map[name]);
422
0
    }
423
424
32
    DCHECK(parent_counter_name == ROOT_COUNTER ||
425
32
           _counter_map.find(parent_counter_name) != _counter_map.end());
426
32
    NonZeroCounter* counter = _pool->add(new NonZeroCounter(type, level, parent_counter_name));
427
32
    _counter_map[name] = counter;
428
32
    std::set<std::string>* child_counters =
429
32
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
430
32
    child_counters->insert(name);
431
32
    return counter;
432
32
}
433
434
RuntimeProfile::DerivedCounter* RuntimeProfile::add_derived_counter(
435
        const std::string& name, TUnit::type type, const DerivedCounterFunction& counter_fn,
436
8
        const std::string& parent_counter_name) {
437
8
    std::lock_guard<std::mutex> l(_counter_map_lock);
438
439
8
    if (_counter_map.find(name) != _counter_map.end()) {
440
0
        return nullptr;
441
0
    }
442
443
8
    DerivedCounter* counter = _pool->add(new DerivedCounter(type, counter_fn));
444
8
    _counter_map[name] = counter;
445
8
    std::set<std::string>* child_counters =
446
8
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
447
8
    child_counters->insert(name);
448
8
    return counter;
449
8
}
450
451
void RuntimeProfile::add_description(const std::string& name, const std::string& description,
452
78.0k
                                     std::string parent_counter_name) {
453
78.0k
    std::lock_guard<std::mutex> l(_counter_map_lock);
454
455
78.0k
    if (_counter_map.find(name) != _counter_map.end()) {
456
0
        Counter* counter = _counter_map[name];
457
0
        if (dynamic_cast<DescriptionEntry*>(counter) != nullptr) {
458
            // Do replace instead of update to avoid data race.
459
0
            _counter_map.erase(name);
460
0
        } else {
461
0
            DCHECK(false) << "Counter type mismatch, name: " << name
462
0
                          << ", type: " << counter->type() << ", description: " << description;
463
0
        }
464
0
    }
465
466
    // Parent counter must already exist.
467
78.0k
    DCHECK(parent_counter_name == ROOT_COUNTER ||
468
78.0k
           _counter_map.find(parent_counter_name) != _counter_map.end());
469
78.0k
    DescriptionEntry* counter = _pool->add(new DescriptionEntry(name, description));
470
78.0k
    _counter_map[name] = counter;
471
78.0k
    std::set<std::string>* child_counters =
472
78.0k
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
473
78.0k
    child_counters->insert(name);
474
78.0k
}
475
476
RuntimeProfile::ConditionCounter* RuntimeProfile::add_conditition_counter(
477
        const std::string& name, TUnit::type type, const ConditionCounterFunction& counter_fn,
478
0
        const std::string& parent_counter_name, int64_t level) {
479
0
    std::lock_guard<std::mutex> l(_counter_map_lock);
480
481
0
    if (_counter_map.find(name) != _counter_map.end()) {
482
0
        RuntimeProfile::ConditionCounter* contition_counter =
483
0
                dynamic_cast<ConditionCounter*>(_counter_map[name]);
484
0
        if (contition_counter == nullptr) {
485
0
            throw doris::Exception(doris::ErrorCode::INTERNAL_ERROR,
486
0
                                   "Failed to add a conditition counter that is duplicate and of a "
487
0
                                   "different type for {}.",
488
0
                                   name);
489
0
        }
490
0
        return contition_counter;
491
0
    }
492
493
0
    ConditionCounter* counter = _pool->add(new ConditionCounter(type, counter_fn, level));
494
0
    _counter_map[name] = counter;
495
0
    std::set<std::string>* child_counters =
496
0
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
497
0
    child_counters->insert(name);
498
0
    return counter;
499
0
}
500
501
3.25k
RuntimeProfile::Counter* RuntimeProfile::get_counter(const std::string& name) {
502
3.25k
    std::lock_guard<std::mutex> l(_counter_map_lock);
503
504
3.25k
    if (_counter_map.find(name) != _counter_map.end()) {
505
2.81k
        return _counter_map[name];
506
2.81k
    }
507
508
440
    return nullptr;
509
3.25k
}
510
511
0
void RuntimeProfile::get_counters(const std::string& name, std::vector<Counter*>* counters) {
512
0
    Counter* c = get_counter(name);
513
514
0
    if (c != nullptr) {
515
0
        counters->push_back(c);
516
0
    }
517
518
0
    std::lock_guard<std::mutex> l(_children_lock);
519
520
0
    for (int i = 0; i < _children.size(); ++i) {
521
0
        _children[i].first->get_counters(name, counters);
522
0
    }
523
0
}
524
525
// Print the profile:
526
//  1. Profile Name
527
//  2. Info Strings
528
//  3. Counters
529
//  4. Children
530
void RuntimeProfile::pretty_print(std::ostream* s, const std::string& prefix,
531
65
                                  int64_t profile_level) const {
532
65
    std::ostream& stream = *s;
533
534
    // create copy of _counter_map and _child_counter_map so we don't need to hold lock
535
    // while we call value() on the counters
536
65
    CounterMap counter_map;
537
65
    ChildCounterMap child_counter_map;
538
65
    {
539
65
        std::lock_guard<std::mutex> l(_counter_map_lock);
540
65
        counter_map = _counter_map;
541
65
        child_counter_map = _child_counter_map;
542
65
    }
543
544
65
    std::map<std::string, Counter*>::const_iterator total_time = counter_map.find("TotalTime");
545
65
    DCHECK(total_time != counter_map.end());
546
547
65
    stream.flags(std::ios::fixed);
548
65
    stream << prefix << _name << ":";
549
550
65
    if (total_time->second->value() != 0) {
551
0
        stream << "(Active: "
552
0
               << PrettyPrinter::print(total_time->second->value(), total_time->second->type())
553
0
               << ", non-child: " << std::setprecision(2) << _local_time_percent << "%)";
554
0
    }
555
556
65
    stream << std::endl;
557
558
65
    {
559
65
        std::lock_guard<std::mutex> l(_info_strings_lock);
560
65
        for (const std::string& key : _info_strings_display_order) {
561
2
            stream << prefix << "   - " << key << ": " << _info_strings.find(key)->second
562
2
                   << std::endl;
563
2
        }
564
65
    }
565
566
65
    RuntimeProfile::print_child_counters(prefix, ROOT_COUNTER, counter_map, child_counter_map, s);
567
568
    // create copy of _children so we don't need to hold lock while we call
569
    // pretty_print() on the children
570
65
    ChildVector children;
571
65
    {
572
65
        std::lock_guard<std::mutex> l(_children_lock);
573
65
        children = _children;
574
65
    }
575
576
105
    for (int i = 0; i < children.size(); ++i) {
577
40
        RuntimeProfile* profile = children[i].first;
578
40
        bool indent = children[i].second;
579
40
        profile->pretty_print(s, prefix + (indent ? "  " : ""));
580
40
    }
581
65
}
582
583
237
void RuntimeProfile::to_thrift(TRuntimeProfileTree* tree, int64 profile_level) {
584
237
    tree->nodes.clear();
585
237
    to_thrift(&tree->nodes, profile_level);
586
237
}
587
588
245
void RuntimeProfile::to_thrift(std::vector<TRuntimeProfileNode>* nodes, int64 profile_level) {
589
245
    int index = nodes->size();
590
245
    nodes->push_back(TRuntimeProfileNode());
591
245
    TRuntimeProfileNode& node = (*nodes)[index];
592
245
    node.name = _name;
593
245
    node.metadata = _metadata;
594
245
    node.timestamp = _timestamp;
595
245
    node.indent = true;
596
597
245
    {
598
245
        std::lock_guard<std::mutex> l(_counter_map_lock);
599
245
        RuntimeProfileCounterTreeNode conter_tree = RuntimeProfileCounterTreeNode::from_map(
600
245
                _counter_map, _child_counter_map, ROOT_COUNTER);
601
245
        conter_tree = RuntimeProfileCounterTreeNode::prune_the_tree(conter_tree, profile_level);
602
245
        conter_tree.to_thrift(node.counters, node.child_counters_map);
603
245
    }
604
605
245
    {
606
245
        std::lock_guard<std::mutex> l(_info_strings_lock);
607
245
        node.info_strings = _info_strings;
608
245
        node.info_strings_display_order = _info_strings_display_order;
609
245
    }
610
611
245
    ChildVector children;
612
245
    {
613
        // _children may be modified during to_thrift(),
614
        // so we have to lock and copy _children to avoid race condition
615
245
        std::lock_guard<std::mutex> l(_children_lock);
616
245
        children = _children;
617
245
    }
618
245
    node.num_children = children.size();
619
245
    nodes->reserve(nodes->size() + children.size());
620
621
251
    for (int i = 0; i < children.size(); ++i) {
622
6
        int child_idx = nodes->size();
623
6
        children[i].first->to_thrift(nodes, profile_level);
624
        // fix up indentation flag
625
6
        (*nodes)[child_idx].indent = children[i].second;
626
6
    }
627
245
}
628
629
int64_t RuntimeProfile::units_per_second(const RuntimeProfile::Counter* total_counter,
630
3
                                         const RuntimeProfile::Counter* timer) {
631
3
    DCHECK(total_counter->type() == TUnit::BYTES || total_counter->type() == TUnit::UNIT);
632
3
    DCHECK(timer->type() == TUnit::TIME_NS);
633
634
3
    if (timer->value() == 0) {
635
0
        return 0;
636
0
    }
637
638
3
    double secs = static_cast<double>(timer->value()) / 1000.0 / 1000.0 / 1000.0;
639
3
    return int64_t(total_counter->value() / secs);
640
3
}
641
642
0
int64_t RuntimeProfile::counter_sum(const std::vector<Counter*>* counters) {
643
0
    int64_t value = 0;
644
645
0
    for (int i = 0; i < counters->size(); ++i) {
646
0
        value += (*counters)[i]->value();
647
0
    }
648
649
0
    return value;
650
0
}
651
652
void RuntimeProfile::print_child_counters(const std::string& prefix,
653
                                          const std::string& counter_name,
654
                                          const CounterMap& counter_map,
655
                                          const ChildCounterMap& child_counter_map,
656
445
                                          std::ostream* s) {
657
445
    auto itr = child_counter_map.find(counter_name);
658
659
445
    if (itr != child_counter_map.end()) {
660
54
        const std::set<std::string>& child_counters = itr->second;
661
380
        for (const std::string& child_counter : child_counters) {
662
380
            auto iter = counter_map.find(child_counter);
663
380
            DCHECK(iter != counter_map.end());
664
380
            iter->second->pretty_print(s, prefix, iter->first);
665
380
            RuntimeProfile::print_child_counters(prefix + "  ", child_counter, counter_map,
666
380
                                                 child_counter_map, s);
667
380
        }
668
54
    }
669
445
}
670
671
} // namespace doris